Feedback

C# - Add/Remove registry entries for windows startup.

Veröffentlicht von am 1/21/2007
(3 Bewertungen)
Add/Remove registry entries for windows startup.
/// <summary>
/// Add/Remove registry entries for windows startup.
/// </summary>
/// <param name="AppName">Name of the application.</param>
/// <param name="enable">if set to <c>true</c> [enable].</param>
private void SetStartup(string AppName, bool enable)
{
    string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

    Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey);
   
    if (enable)
    {
        if (startupKey.GetValue(AppName) == null)
        {
            startupKey.Close();
            startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
            // Add startup reg key
            startupKey.SetValue(AppName, Application.ExecutablePath.ToString());
            startupKey.Close();
        }
    }
    else
    {
        // remove startup
        startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
        startupKey.DeleteValue(AppName, false);
        startupKey.Close();
    }
}
Abgelegt unter Registry, Autostart.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!