Erik Porter (gravatar)

Have your app startup when user logs in

This is simple, but I thought it was kind of neat.

Public Class Common

     Private 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

  • Hi Erik, Thanks for the nice tip. I'm using it in my app right now and works fine. Thanks, S. Srinivasa Sivakumar

  • Great, glad you got some use out of it! :)

  • http&#58;&#47;&#47; (gravatar)

    http:// said
    December 24, 2003

    Very nice tip, I like to start this on startup and I wanted to add it to &quot;Tray&quot; with some Icon. Could it also be possible!

  • Of course...look at using the NotifyIcon Component in your application and it will put it down in the tray for you.

  • 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.

  • 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 :)

Your Information
Mrs. Gravatar (gravatar)

<-- It's a gravatar

your comment