Автозагрузка в C#

29.03.2015 at 14:03
internal class AutorunTools
{
    private const string AppName = "MyApplication";
    public static bool IsStartupItem()
    {
        var rkApp = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
            "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
        return rkApp.GetValue(AutorunTools.AppName) != null;
    }


    public static void AddToAutorun()
    {
        var rkApp = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
            "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
        if (!IsStartupItem())
            rkApp.SetValue(AutorunTools.AppName, System.Windows.Forms.Application.ExecutablePath.ToString());
    }


    public static void DeleteFromAutorun()
    {
        var rkApp = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
            "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
        if (IsStartupItem())
            rkApp.DeleteValue(AutorunTools.AppName, false);
    }
}