OK here's my tutorial on how to get YOUR program to startup when Windows does.
Now, since we are accessing the registry, you will need to import Microsoft.Win32, which contains the part of the .NET Framework that handles the registry.
There are two different registry directories that handle startup items, HKEY_Current_User and HKEY_Local_Machine. Current User will start the program for the current user only, and the Local Machine will start the program for anyone who uses the computer.
To add a startup item to the Current_User, you will have to open the registry key and set the value, like so:
Code:
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 Sub
And to remove it:
Code:
Private Sub RemoveCurrentKey(ByVal name As String)
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
key.DeleteValue(name, False)
End Sub
Okay thats all well and good but how would you use it?
Well, I am using a Check Box to determine wether or not the registry startup key is set. The "name" and "path" will be set when the function that sets the registry value is called.
Code:
If CurrentStartup.Checked Then
AddCurrentKey("StartupExample", System.Reflection.Assembly.GetEntryAssembly.Location)
Else
RemoveCurrentKey("StartupExample")
End If
Definitions:
StartupExample is the name of the project, and will also be the name set in the registry key. System.Reflection.Assembly.GetEntryAssembly.Location gets the current location of the program to store in the registry key.
Also, another way that you can set the "name" of the startup key other than specifying it, is by using System.Reflection.Assembly.GetEntryAssembly.FullName.
Now, to set the same startup key in the Local_Machine, just change CurrentUser to LocalMachine
(please remember that the registry is like an animal if you pull its bones out it will cease to work, so dont mess it up!
)
Enjoy