C# 设定APP开机自启动

C#软件开机自启服务

经过作者整合测试,此方法将启动项加入到注册表,多次测试流畅。
废话不多说,直接上代码

添加注册表

 public static bool setAutoStart(string softwareName, string softwarePath)
        {
            if (null == softwareName || 0 == softwareName.Trim().Length)
            {
                Console.WriteLine("invalid softwareName: " + softwareName);
                return false;
            }

            if (null == softwarePath || 0 == softwarePath.Trim().Length)
            {
                Console.WriteLine("invalid softwarePath: " + softwarePath);
                return false;
            }

            RegistryKey localMachineKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
            RegistryKey regKeyRun = localMachineKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
            RegistryKey regKeySoftware = null, regKeyMiscrosoft = null, regKeyWindows = null, regKeyCurrentVersion = null;
            try
            {
                if (null == regKeyRun)
                {
                    Console.WriteLine("openSubKey failed. try create it.");
                    regKeySoftware = localMachineKey.CreateSubKey("Software");
                    regKeyMiscrosoft = regKeySoftware.CreateSubKey("Microsoft");
                    regKeyWindows = regKeyMiscrosoft.CreateSubKey("Windows");
                    regKeyCurrentVersion = regKeyWindows.CreateSubKey("CurrentVersion");
                    regKeyRun = regKeyCurrentVersion.CreateSubKey("Run");
                }

                string keyName = getAutoStartKeyName(softwareName, softwarePath);
                return WriteRegistryValue(regKeyRun, keyName, softwarePath);
            }
            finally
            {
                SafeCloseRegKey(regKeyRun); regKeyRun = null;
                SafeCloseRegKey(regKeyCurrentVersion); regKeyCurrentVersion = null;
                SafeCloseRegKey(regKeyWindows); regKeyWindows = null;
                SafeCloseRegKey(regKeyMiscrosoft); regKeyMiscrosoft = null;
                SafeCloseRegKey(regKeySoftware); regKeySoftware = null;
            }
        }

其他代码

         private static void SafeCloseRegKey(RegistryKey regKey)
        {
            if (null != regKey)
            {
                regKey.Close();
            }
        }

        public static string getAutoStartKeyName(string softwareName, string softwarePath)
        {
            if (null == softwareName || 0 == softwareName.Trim().Length)
            {
                return "";
            }
            if (null == softwarePath || 0 == softwarePath.Trim().Length)
            {
                return "";
            }
            string md5sum = Md5sum(softwarePath);
            return softwareName + "_" + md5sum.Substring(0, 8);
        }
        private static bool WriteRegistryValue(RegistryKey regKey, string keyName, string value)
        {
            if (null == keyName || 0 == keyName.Trim().Length)
            {
                Console.WriteLine("WriteRegistryValue, invalid keyName: " + keyName);
                return false;
            }
            if (null == value || 0 == value.Trim().Length)
            {
                Console.WriteLine("WriteRegistryValue, invalid value: " + value);
                return false;
            }
            try
            {
                bool needChange = false;
                string[] names = regKey.GetValueNames();
                if (null != names)
                {
                    for (int i = 0; i < names.Length; i++)
                    {
                        string oldValue = (string)regKey.GetValue(keyName, "");
                        if (null == oldValue || !oldValue.Equals(value))
                        {
                            needChange = true;
                            break;
                        }
                    }
                }
                if (needChange)
                {
                    regKey.SetValue(keyName, value);
                }
                return true;
            }
            catch (SecurityException e)
            {
                Console.WriteLine("WriteRegistryValue, SecurityException ");
            }
            catch (ObjectDisposedException e)
            {
                Console.WriteLine("WriteRegistryValue, ObjectDisposedException");
            }
            catch (IOException e)
            {
                Console.WriteLine("WriteRegistryValue, IOException");
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine("WriteRegistryValue, IOException");
            }
            return false;
        }
        public static string Md5sum(string input)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();

            byte[] inputBytes = Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }

            return sb.ToString();
        }

源码下载CS文件(包含删除+查询方法)

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Angle_Mjd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值