Exemple #1
0
        public static bool TrySetLaunchAtStartup(bool enabled)
        {
            string name = Application.ProductName;
            string path = Application.ExecutablePath.ToString();

            if (name.IsNullOrEmpty() || path.IsNullOrEmpty())
            {
                return(false);
            }

            path = "\"" + path + "\"";
            MW32.RegistryKey RKey_CU     = MW32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            MW32.RegistryKey RKey_LM     = MW32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            MW32.RegistryKey RKey_LM_wow = MW32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            MW32.RegistryKey RKey_LM_32  = MW32.RegistryKey.OpenBaseKey(MW32.RegistryHive.LocalMachine, MW32.RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            MW32.RegistryKey RKey_LM_64  = MW32.RegistryKey.OpenBaseKey(MW32.RegistryHive.LocalMachine, MW32.RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (enabled)
            {
                RKey_CU.TrySetValue(name, path);
                RKey_LM.TrySetValue(name, path);
                RKey_LM_wow.TrySetValue(name, path);
                RKey_LM_32.TrySetValue(name, path);
                RKey_LM_64.TrySetValue(name, path);
            }
            else
            {
                RKey_CU.TryDeleteValue(name, false);
                RKey_LM.TryDeleteValue(name, false);
                RKey_LM_wow.TryDeleteValue(name, false);
                RKey_LM_32.TryDeleteValue(name, false);
                RKey_LM_64.TryDeleteValue(name, false);
            }

            return(true);
        }