c#设置成开机启动


    class AutoStartup
    {
        public static bool Set(bool enabled)
        {
            try
            {
                string path = Application.ExecutablePath;
                RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
                if (enabled)
                {
                    runKey.SetValue("Shadowsocks", path);
                }
                else
                {
                    runKey.DeleteValue("Shadowsocks");
                }
                runKey.Close();
                return true;
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
                return false;
            }
        }

        public static bool Check()
        {
            try
            {
                string path = Application.ExecutablePath;
                RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                string[] runList = runKey.GetValueNames();
                runKey.Close();
                foreach (string item in runList)
                {
                    if (item.Equals("Shadowsocks"))
                        return true;
                }
                return false;
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
                return false;
            }
        }
    }

以上代码是copy自shadowsocks的c#代码:链接

将程序名“Shadowsocks”改成自己的程序名即可。方法Set(bool enable)为自动启动设置,开启或者关闭。Check则为检验

猜你喜欢

转载自blog.csdn.net/fenglifeng1987/article/details/42459067