-
Feb 8th, 2016, 04:08 PM
#1
Thread Starter
Lively Member
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.
-
Feb 8th, 2016, 04:28 PM
#2
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
-
Feb 8th, 2016, 04:31 PM
#3
Thread Starter
Lively Member
Re: Start Program At Startup With Registry
The line this is occuring on is:
Code:
regkey.SetValue(ValueName, Value, RegistryValueKind.String)
-
Feb 8th, 2016, 04:33 PM
#4
Thread Starter
Lively Member
Re: Start Program At Startup With Registry
Yes, when I select Shift+F9 the value is equal to nothing. How do I fix this?
-
Feb 8th, 2016, 04:51 PM
#5
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.
-
Feb 8th, 2016, 04:55 PM
#6
Thread Starter
Lively Member
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.
-
Feb 8th, 2016, 05:06 PM
#7
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.
-
Feb 8th, 2016, 05:14 PM
#8
Thread Starter
Lively Member
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?
-
Feb 8th, 2016, 05:26 PM
#9
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
-
Feb 8th, 2016, 05:27 PM
#10
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.
-
Feb 8th, 2016, 05:45 PM
#11
Thread Starter
Lively Member
Re: Start Program At Startup With Registry
My Windows 10 Laptop is running Framwork 4.6 and my windows 7 is running 4.5
-
Feb 8th, 2016, 05:47 PM
#12
Thread Starter
Lively Member
Re: Start Program At Startup With Registry
Originally Posted by Sitten Spynne
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.
-
Feb 9th, 2016, 02:38 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|