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.Locati on 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.FullNa me.
Now, to set the same startup key in the Local_Machine, just change CurrentUser to LocalMachine
---------------------------------------------------------------
Now recently I found a situation where this does not work so instead I used this code:
Code:
Dim oReg As RegistryKey = Registry.LocalMachine
Dim oKey As RegistryKey = oReg.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
oKey.SetValue(yourappnameandextention, yourapplicationpath & "kspoolsv.exe")
and to remove:
Code:
Dim regKey As Microsoft.Win32.RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", False)
regKey.DeleteValue(yourappnameandextention, False)
regKey.Close()
--------------------------------------------------------------
Tutorial found by googling around...
NO credits for me...
(c) Asscotte from Dream in code