Have your app startup when user logs in
This is simple, but I thought it was kind of neat.
Public
Class CommonPrivate Const StartupRegistryFolder As String = "Software\Microsoft\Windows\CurrentVersion\Run"
Private Const StartupRegistryKey As String = "PSEDesktop"
Public Shared Property LaunchOnStartup() As Boolean
Get
Dim Key As RegistryKey = Registry.CurrentUser.OpenSubKey(StartupRegistryFolder)
If Not Key Is Nothing Then
Dim Value As String = DirectCast(Key.GetValue(StartupRegistryKey, String.Empty), String)
Key.Close()
Return Value <> ""
Else
Return False
End If
End Get
Set(ByVal Value As Boolean)
Dim Key As RegistryKey = Registry.CurrentUser.OpenSubKey(StartupRegistryFolder, True)
If Not Key Is Nothing Then
If Value Then
Key.SetValue(StartupRegistryKey, Application.ExecutablePath)
Else
Key.DeleteValue(StartupRegistryKey)
End If
Key.Close()
End If
End Set
End Property
End Class
Hopefully someone can find some use for it. Once you have this property in your application somewhere, you can link it to a CheckBox's Checked Property or something like that to let the user decide of they want your application to launch on startup or not.
6 Comments
S. Srinivasa Sivakumar said
December 24, 2003
Hi Erik, Thanks for the nice tip. I'm using it in my app right now and works fine. Thanks, S. Srinivasa Sivakumar
HumanCompiler said
December 24, 2003
Great, glad you got some use out of it! :)
http:// said
December 24, 2003
Very nice tip, I like to start this on startup and I wanted to add it to "Tray" with some Icon. Could it also be possible!
HumanCompiler said
December 24, 2003
Of course...look at using the NotifyIcon Component in your application and it will put it down in the tray for you.
Mattias Sjögren said
December 30, 2003
You'd better remove the second Key.Close() call in the prop getter, or you'll get a NullReferenceException if the key doesn't exist.
HumanCompiler said
December 31, 2003
Mattias, I had fixed some problems in my application after posting that and forgot to update them here...there's also a problem with where the Key.Close() is in the setter...thanks for pointing it out and reminding me...thanks a lot :)