zoukankan      html  css  js  c++  java
  • C#中设置开机自动运行和关机

    让软件开机自动运行或者设置自动关机,大部分软件都有这种功能。如何实现呢,其实很简单,开机运行,只需要设置注册表就可以了,关机则调用CMD命令:shutdown -s -t,如下:
    开机自动运行:


    /// <summary>  
    /// 设置开机运行  
     /// </summary>  
    public void AutoRun() 

        RegistryKey runItem = 
        Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); 
     
        if (runItem == null) 
        { 
            run.SetValue("exe的名字","exe的路径"); 
        } 

    /// <summary>  
    /// 取消开机运行  
     /// </summary>  
    public void DeleteAutoRun() 

        RegistryKey runItem = 
        Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); 
     
        if (runItem != null) 
        { 
            runItem.DeleteSubKey("exe的名字"); 
        } 

            /// <summary>
            /// 设置开机运行
             /// </summary>
            public void AutoRun()
            {
                RegistryKey runItem =
                Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);

                if (runItem == null)
                {
                    run.SetValue("exe的名字","exe的路径");
                }
            }
            /// <summary>
            /// 取消开机运行
             /// </summary>
            public void DeleteAutoRun()
            {
                RegistryKey runItem =
                Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);

                if (runItem != null)
                {
                    runItem.DeleteSubKey("exe的名字");
                }
            }
     
    设置关机:www.2cto.com


            public static string ExecuteCmd(string command) 
            { 
                string output = ""; //输出字符串     
                if (command != null && !command.Equals("")) 
                { 
                    Process process = new Process();//创建进程对象     
                    ProcessStartInfo startInfo = new ProcessStartInfo(); 
                    startInfo.FileName = "cmd.exe";//设定需要执行的命令     
                    startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出     
                    startInfo.UseShellExecute = false;//不使用系统外壳程序启动     
                    startInfo.RedirectStandardInput = false;//不重定向输入     
                    startInfo.RedirectStandardOutput = true; //重定向输出     
                    startInfo.CreateNoWindow = true;//不创建窗口     
                    process.StartInfo = startInfo; 
                    process.Start(); 
                } 
            } 
            public static string ExecuteCmd(string command)
            {
                string output = ""; //输出字符串  
                if (command != null && !command.Equals(""))
                {
                    Process process = new Process();//创建进程对象  
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.FileName = "cmd.exe";//设定需要执行的命令  
                    startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出  
                    startInfo.UseShellExecute = false;//不使用系统外壳程序启动  
                    startInfo.RedirectStandardInput = false;//不重定向输入  
                    startInfo.RedirectStandardOutput = true; //重定向输出  
                    startInfo.CreateNoWindow = true;//不创建窗口  
                    process.StartInfo = startInfo;
                    process.Start();
                }
            }
    调用:


    ExecuteCmd("shutdown -s -t"); 
    ExecuteCmd("shutdown -s -t");

  • 相关阅读:
    还做开发!重新学习纪念一下先
    NOD32中小企业服务器版部署方法
    我买车了,写个总结
    Windows Server 2008 各个版本微软官方下载
    SQLServer2008过程中因性能计数器不一致导致无法安装的解决方法
    自建邮件服务器的注意事项
    01.Linux下C语言编程环境检查
    wcf部署到IIS宿主上报错
    Win7 开发WCF时 提示 进程不具有此命名空间的访问权限
    SQLServer2008设置 开启远程连接 (转)
  • 原文地址:https://www.cnblogs.com/zhihai/p/2349577.html
Copyright © 2011-2022 走看看