zoukankan      html  css  js  c++  java
  • WinForm设置注册表自动启动

    string path = Application.StartupPath;
                SetAutoRun(path + @"AppName.exe", true);
    
     /// <summary>
            /// 设置应用程序开机自动运行
            /// </summary>
            /// <param name="fileName">应用程序的文件名</param>
            /// <param name="isAutoRun">是否自动运行,为false时,取消自动运行</param>
            /// <exception cref="system.Exception">设置不成功时抛出异常</exception>
            /// <returns>返回1成功,非1不成功</returns>
            public String SetAutoRun(string fileName, bool isAutoRun)
            {
                string reSet = string.Empty;
                RegistryKey reg = null;
                try
                {
                    if (!System.IO.File.Exists(fileName))
                    {
                        reSet = "该文件不存在!";
                    }
                    string name = fileName.Substring(fileName.LastIndexOf(@"") + 1);
                    reg = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun", true);
                    if (reg == null)
                    {
                        reg = Registry.LocalMachine.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
                    }
                    if (isAutoRun)
                    {
                        reg.SetValue(name, fileName);
                        reSet = "1";
                    }
                    else
                    {
                        reg.SetValue(name, false);
                    }
    
    
                }
                catch (Exception ex)
                {
                    threadMethod("设置自动启动注册表失败:" + ex.Message);
                }
                finally
                {
                    if (reg != null)
                    {
                        reg.Close();
                    }
                }
                return reSet;
            }
  • 相关阅读:
    poj3436(ACM Computer Factory)
    一位ACMer过来人的心得
    poj1459(Power Network)
    (转)网络流—最大流(Edmond-Karp算法)
    poj1611(The Suspects)
    构建之法阅读笔记01
    第三周总结
    全国疫情可视化地图
    第二周总结
    作业--数组(大数)
  • 原文地址:https://www.cnblogs.com/tangchun/p/9967959.html
Copyright © 2011-2022 走看看