Make a .NET application run at startup
Only applicable to .Net desktop application. For .NET compact framework application, use SHCreateShortcut and creates a shortcut in WindowsStartup folder.
‘name of application to be set in registry key
Private Const appname As String = “MyAppName”
‘check whether or not the application is set to run at startup via registry key
‘return TRUE if ok, FALSE if error
Public Function IsRunAtStartup() As Boolean
Try
Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(“SOFTWARE\Microsoft\Windows\CurrentVersion\Run”, False)
Dim keyValue As Object = key.GetValue(appname)
Return keyValue IsNot Nothing
Catch ex As Exception
writeLog(“Error @IsRunAtStartup: ” + ex.Message)
Return False
End Try
End Function
‘set whether or not the application is set to run at startup via registry key
‘return TRUE if ok, FALSE if error
Public Function SetRunAtStarup(ByVal runStartup As Boolean) As Boolean
Try
Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(“SOFTWAREMicrosoftWindowsCurrentVersionRun”, True)
If runStartup Then
key.SetValue(appname, Application.ExecutablePath.ToString)
Else
key.DeleteValue(appname)
End If
Return True
Catch ex As Exception
writeLog(“Error @SetRunAtStarup: ” + ex.Message)
Return False
End Try
End Function