Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    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.

    Comments

    S. Srinivasa Sivakumar said:

    Hi Erik,

    Thanks for the nice tip. I'm using it in my app right now and works fine.

    Thanks,
    S. Srinivasa Sivakumar
    # December 24, 2003 5:08 AM

    HumanCompiler said:

    Great, glad you got some use out of it! :)
    # December 24, 2003 5:48 AM

    ravi said:

    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!
    # December 24, 2003 10:04 AM

    HumanCompiler said:

    Of course...look at using the NotifyIcon Component in your application and it will put it down in the tray for you.
    # December 24, 2003 2:37 PM

    Mattias Sjögren said:

    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.
    # December 31, 2003 1:11 AM

    HumanCompiler said:

    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 :)
    # December 31, 2003 3:22 PM