Results 1 to 13 of 13

Thread: Start Program At Startup With Registry

  1. #1

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Start Program At Startup With Registry

    I am currently trying to get my program to start up at Windows Startup. However, every code I create or find, I get errors thrown at me. I can't seem to get this to work no matter how many times I change my codes. Could someone please help me?

    Here is my code:

    Code:
    Imports Microsoft.Win32
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim regkey As Microsoft.Win32.RegistryKey
            Dim Keyname As String = "SOFTWARE\Microsoft\Windows*\CurrentVersion\Run" 'Registrypath
            Dim ValueName As String = Application.ProductName 'Program name
            Dim Value As String = Application.ExecutablePath
            'Create Startup
            regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(Keyname, True)
            regkey.SetValue(ValueName, Value, RegistryValueKind.String)
        End Sub
    End Class

    Here is the error that is thrown at me every time I try to run the function:
    An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication3.exe

    Additional information: Object reference not set to an instance of an object.


    How can I fix this? I know this should have been easy and fairly simple to do. I've been trying to work just this one function for hours. Any help would be greatly appreciated!
    Last edited by kshadow22; Feb 8th, 2016 at 04:33 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Start Program At Startup With Registry

    The key to that exception is finding the object that is Nothing, then figuring out why it is Nothing. So, when the exception happens, you SHOULD be taken to the line that has the object in question. There are times when you are not taken to the right line, but I'm pretty sure this is not one of those situations, so you should have the problem line identified. I would guess that the exception is occurring on this line:

    regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(Keyname, True)

    in which case, the problem is likely this part:

    Microsoft.Win32.Registry.CurrentUser

    So, if that is the line with the exception, then highlight that part and press Shift+F9 to see whether it is Nothing. If that isn't the line in question, then tell us which line the problem IS on, and which object is Nothing.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Re: Start Program At Startup With Registry

    The line this is occuring on is:

    Code:
     regkey.SetValue(ValueName, Value, RegistryValueKind.String)

  4. #4

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Re: Start Program At Startup With Registry

    Yes, when I select Shift+F9 the value is equal to nothing. How do I fix this?
    Name:  Capture.PNG
Views: 215
Size:  3.8 KB

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Start Program At Startup With Registry

    Use your thinking parts!

    regkey is set to the result of an OpenSubKey() call. It's Nothing. So you have to ask OpenSubKey() when its value will be Nothing. You do that by looking in the documentation. Here's what it says:
    Return Value
    Type: Microsoft.Win32.RegistryKey
    The subkey requested, or null if the operation failed.
    Hm. So it can return Nothing if the operation failed. Why might the operation fail? One reason is if the indicated key doesn't exist. I was going to blame security settings, but look at the path again:
    Code:
     "SOFTWARE\Microsoft\Windows*\CurrentVersion\Run"
    Something is wrong with that path. Have another look and see if you can find it.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  6. #6

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Re: Start Program At Startup With Registry

    Oh gosh, I actually don't know how that got there, because it isn't actually there in my coding inside VS. So its not the problem. I must have accidentally hit that key while making the thread.

    Name:  Capture.jpg
Views: 172
Size:  19.6 KB

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Start Program At Startup With Registry

    I hate to say "it works on my machine", but it does. I'm on crotchety old Windows 7. What version are you using?
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Re: Start Program At Startup With Registry

    I am on Windows 10, I happen to have a Windows 7 beside me, I will give it a try on that and let you know how it runs. Which Visual Studio version are you using?

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Start Program At Startup With Registry

    Actually, which version of the framework are you using? The version of VS often doesn't matter nearly as much, though if you are using an older version then your choices of framework will be reduced.
    My usual boring signature: Nothing

  10. #10
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Start Program At Startup With Registry

    I'm curious if, on Windows 10, you need to have UAC elevation to write to those keys. In general, it is a bad behavior for a program to register itself for startup, because bad programs will automatically add themselves every time, even if a user doesn't want them there. The right tool for this is probably an installer.

    You might try running your application with UAC elevation and see if it magically works. If that's the case, then there's not going to be an easy code-based solution for this.

    For reference, I'm using VS 2015, but this particular bit of .NET hasn't changed much since at least .NET 2.0 and probably earlier.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  11. #11

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Re: Start Program At Startup With Registry

    My Windows 10 Laptop is running Framwork 4.6 and my windows 7 is running 4.5

  12. #12

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Re: Start Program At Startup With Registry

    Quote Originally Posted by Sitten Spynne View Post
    I'm curious if, on Windows 10, you need to have UAC elevation to write to those keys. In general, it is a bad behavior for a program to register itself for startup, because bad programs will automatically add themselves every time, even if a user doesn't want them there. The right tool for this is probably an installer.

    You might try running your application with UAC elevation and see if it magically works. If that's the case, then there's not going to be an easy code-based solution for this.

    For reference, I'm using VS 2015, but this particular bit of .NET hasn't changed much since at least .NET 2.0 and probably earlier.

    I tried both Windows 7 and Windows 10 (both with different framework version) and still did not work. I get the same error. I have my program running as administrator by default, and I have even tried it without admin.

  13. #13
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Start Program At Startup With Registry

    I always thought uac dialogs were blocked at startup windows 7 http://blogs.msdn.com/b/uac/archive/...23/715265.aspx

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width