zoukankan      html  css  js  c++  java
  • 操作注册表,实现软件的开机自启动

    感谢:

    WPF群(WPF 4658393)的 

    【总裁】北京-男-雅蠛蝶(2855892xxx) 11:47:35 

        /// <summary>
        /// 操作注册表,实现软件的开机自启动
        /// </summary>
        class MyClass
        {
            public static bool IsExisiteWhenStart(string pExeName = null, string path = null)
            {
                var zpath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                pExeName = Process.GetCurrentProcess().MainModule.FileName;
                pExeName = "监控";
                RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVertion\Run");
    
                return key.GetValue(pExeName, null) != null;
            }
    
            public static Exception RunWhenStart(bool started, string exeName = null, string path = null)
            {
                try
                {
                    path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    path = Process.GetCurrentProcess().MainModule.FileName;
                    exeName = "监控";
                    RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVertion\Run", true);
                    if (null == key)  //如果该项不存在的话,创建子项
                    {
                        key = Registry.CurrentUser.CreateSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    }
    
                    if (started)
                    {
                        key.SetValue(exeName, String.Format(""{0}"", path));  //设置开机启动
                    }
                    else
                    {
                        key.DeleteValue(exeName);  //取消开机启动
                    }
                    key.Close();
                    return null;
                }
                catch (Exception e)
                {
                    return e;
                }
            }
        }

    感谢群:  .Net项目技术组 306485590

    【总监】麦子  254230xxx

            /// <summary>         
            /// 开机启动项        
            /// </summary>       
            /// <param name="Started">是否启动</param>         
            /// <param name="name">启动值的名称</param>          
            /// <param name="path">启动程序的路径</param>         
            public static Boolean RunWhenStart(bool isAutoRun, string fileName)
            {
                RegistryKey reg = null;
                try
                {
                    /*if (!System.IO.File.Exists(fileName)) {
                        Console.WriteLine("注册开机启动失败!原因:文件" + fileName + "不存在!!!");
                        return false;
                    }*/
    
                    String name = fileName.Substring(fileName.LastIndexOf(@"") + 1);
                    reg = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun", true);
                    if (reg == null)
                        reg = Registry.LocalMachine.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
    
                    object regValue = reg.GetValue(name);
    
                    if (null == regValue)
                    {
                        if (isAutoRun)
                        {
                            reg.SetValue(name, fileName);
                            return true;
                        }
                        else
                        {
                            reg.SetValue(name, false);
                            return false;
                        }                        
                    }
                    else
                    {
                        String regValueStr = regValue.ToString();
    
                        if (regValueStr.IndexOf("beetle") == -1)
                        {
                            if (isAutoRun)
                            {
                                reg.SetValue(name, fileName);
                                return true;
                            }                            
                            else
                            {
                                reg.SetValue(name, false);
                                return false;
                            }                            
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("注册开机启动失败!原因:" + ex.ToString() + "!!!");
                    return false;
                }
                finally
                {
                    if (reg != null)
                        reg.Close();
                }
    
            }
    public static void autorun2File(String appPath)
            {
                try
                {
                    if (File.Exists("autorun.info"))
                    {
                        File.Delete("autorun.info");
                    }
    
                    FileStream fs = new FileStream("autorun.info", FileMode.Create, FileAccess.Write); //创建一个文件流对象 
                    byte[] appPathByte = Encoding.Default.GetBytes(appPath);
                    fs.Write(appPathByte, 0, appPathByte.Length);
                    fs.Close(); //关闭文件流 
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
    
            public static void autorun2SysWOW64File()
            {
                try
                {
                    String autorunPath = "C:\Windows\SysWOW64\autorun.info";
    
                    if (File.Exists(autorunPath))
                    {
                        File.Delete(autorunPath);
                    }
    
                    FileStream fs = new FileStream("autorun.info", FileMode.Create, FileAccess.Write); //创建一个文件流对象 
                    byte[] appPathByte = Encoding.Default.GetBytes(autorunPath);
                    fs.Write(appPathByte, 0, appPathByte.Length);
                    fs.Close(); //关闭文件流 
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
    
            public static void autorun2System32File()
            {
                try
                {
                    String autorunPath = "C:\Windows\System32\autorun.info";
    
                    if (File.Exists(autorunPath))
                    {
                        File.Delete(autorunPath);
                    }
    
                    FileStream fs = new FileStream("autorun.info", FileMode.Create, FileAccess.Write); //创建一个文件流对象 
                    byte[] appPathByte = Encoding.Default.GetBytes(autorunPath);
                    fs.Write(appPathByte, 0, appPathByte.Length);
                    fs.Close(); //关闭文件流 
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
    
            public static void autorun2DocumentsSetFile()
            {
                try
                {
                    String autorunPath = "C:\Documents and Settings\Administrator\autorun.info";
    
                    if (File.Exists(autorunPath))
                    {
                        File.Delete(autorunPath);
                    }
    
                    FileStream fs = new FileStream("autorun.info", FileMode.Create, FileAccess.Write); //创建一个文件流对象 
                    byte[] appPathByte = Encoding.Default.GetBytes(autorunPath);
                    fs.Write(appPathByte, 0, appPathByte.Length);
                    fs.Close(); //关闭文件流 
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
  • 相关阅读:
    [转][ASP.NET Core 3框架揭秘] 跨平台开发体验: Windows [上篇]
    [转]Jmeter压力测试工具安装及使用教程
    [转]来自后端的逆袭 blazor简介 全栈的福音
    [转]【译】.NET Core 3.0 中的新变化
    [转]Spring历史版本变迁和如今的生态帝国
    [转]SpringBoot整合Swagger2以及生产环境的安全问题处理
    IIS7下,显示PHP错误(不显示500错误,而显示详细错误)
    移动端FastClick和editor冲突问题。
    npm WARN checkPermissions Missing write access to 解决办法
    教你用Cordova打包Vue项目
  • 原文地址:https://www.cnblogs.com/3Tai/p/4157682.html
Copyright © 2011-2022 走看看