Here's an overloaded function that will write or delete a key in the current user's windows\run subkey in the registry to allow your app to start with windows or to no longer start with windows. This code is specific to the currently logged in user, it doesn't write to the hkey_local_machine.
The first function allows you to specify an application name (the name of the key itself) as well as the path whereas the other function gets the path of the running app as well as the name automatically and passes it in, so all you have to do is specify the "StartWithWindows" boolean.Just drop these into your project (even a module, doesn't matter much) and call either one of them.Code:Public Overloads Function RegStartWithWindows(ByVal StartWithWindows As Boolean) As Boolean Return RegStartWithWindows(StartWithWindows, System.IO.Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs(0I)), Environment.GetCommandLineArgs(0I)) End Function Public Overloads Function RegStartWithWindows(ByVal StartWithWindows As Boolean, ByVal AppName As String, ByVal AppPath As String) As Boolean Dim Output As Boolean = False Dim SubKey As Microsoft.Win32.RegistryKey = Nothing Try SubKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree) If StartWithWindows Then 'Create/Modify SubKey.SetValue(AppName, AppPath) Else 'Delete Dim KeyExists As Boolean = False For Each strkey As String In SubKey.GetValueNames If strkey = AppName Then KeyExists = True 'It exisits, it can be deleted Next strkey If KeyExists Then SubKey.DeleteValue(AppName) 'Delete since it exists End If Output = True Catch ex As Exception MessageBox.Show(ex.ToString, "WriteRegistry") Finally If SubKey IsNot Nothing Then SubKey.Close() End Try Return Output End Function


Reply With Quote