Query regarding the code in MatixCoder's "Autorun Applications" CodeProject article?
-
In the application I'm developing, I'd like to give the user the option to automatically start the application when windows starts. From my searches it looks like setting a registry key is one of the best ways to do it. I haven't ever played with the Registry in my programs before (I've only ever manually edited the registry), so I want to make sure I get it right. I came across MatrixCoder's article "Autorun Applications" which is quite clear and straight forward. He uses the following sub and passes it
System.Reflection.Assembly.GetEntryAssembly.Location
forpath
in order to set the registry key.Private Sub AddCurrentKey(ByVal name As String, ByVal path As String)
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
key.SetValue(name, path)
End SubIf the key already exists, will this just overwrite it, or will it add another key each time
AddCurrentKey()
is called? If it would keep on adding further keys, how would the code best be modified to check if the key already exists? Regards Paul Haslermodified on Tuesday, January 19, 2010 5:03 AM
-
In the application I'm developing, I'd like to give the user the option to automatically start the application when windows starts. From my searches it looks like setting a registry key is one of the best ways to do it. I haven't ever played with the Registry in my programs before (I've only ever manually edited the registry), so I want to make sure I get it right. I came across MatrixCoder's article "Autorun Applications" which is quite clear and straight forward. He uses the following sub and passes it
System.Reflection.Assembly.GetEntryAssembly.Location
forpath
in order to set the registry key.Private Sub AddCurrentKey(ByVal name As String, ByVal path As String)
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
key.SetValue(name, path)
End SubIf the key already exists, will this just overwrite it, or will it add another key each time
AddCurrentKey()
is called? If it would keep on adding further keys, how would the code best be modified to check if the key already exists? Regards Paul Haslermodified on Tuesday, January 19, 2010 5:03 AM
Adding that key will not make your program automatically start when Windows starts. It will make your application start when a user logs on to Windows. When the user logs off the system, the program will be terminated if it is still running. To make your application start when Windows starts, you need to make your application a "Windows Service". If you search for "Windows Service" you will get plenty of information on how to make it. Good luck and happy programming!
-
In the application I'm developing, I'd like to give the user the option to automatically start the application when windows starts. From my searches it looks like setting a registry key is one of the best ways to do it. I haven't ever played with the Registry in my programs before (I've only ever manually edited the registry), so I want to make sure I get it right. I came across MatrixCoder's article "Autorun Applications" which is quite clear and straight forward. He uses the following sub and passes it
System.Reflection.Assembly.GetEntryAssembly.Location
forpath
in order to set the registry key.Private Sub AddCurrentKey(ByVal name As String, ByVal path As String)
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
key.SetValue(name, path)
End SubIf the key already exists, will this just overwrite it, or will it add another key each time
AddCurrentKey()
is called? If it would keep on adding further keys, how would the code best be modified to check if the key already exists? Regards Paul Haslermodified on Tuesday, January 19, 2010 5:03 AM
This doesn't make the application start when Windows starts. This makes it start whenever the user logs in on the machine. If the
name
is the same as an existing value, then that value will get overwritten. it will NOT create another copy of the same name/value pair.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
This doesn't make the application start when Windows starts. This makes it start whenever the user logs in on the machine. If the
name
is the same as an existing value, then that value will get overwritten. it will NOT create another copy of the same name/value pair.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Thank you once again Dave. I used the wrong terminology. I do actually want the application to start when the user logs on. You see so many programs today with preference options such as for example "Start Skype when windows starts", that I fell into the trap of using the same sloppy terminology. With your explanation of how this will behave, I can finish off my app. Cheers again Paul