Make a .NET application run at startup

0.00 avg. rating (0% score) - 0 votes

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

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>