zoukankan      html  css  js  c++  java
  • C# 重启电脑 程序自启动

     public  class ControlComputer
        {
            /// <summary>
            /// 重启当前程序
            /// </summary>
            public static void ReStartApplicationSelf()
            {
                System.Diagnostics.Process.Start(Assembly.GetExecutingAssembly().Location);   
            }
            /// <summary>
            /// 重启电脑
            /// </summary>
            public static void ReStartComputer()
            {
                // 参数 /r 的意思是要重启计算机   
                Process.Start("shutdown", "/r /t 0");
            }
            /// <summary>
            /// 关机
            /// </summary>
            public static void ColseDownComputer()
            {
                // 参数 /s 的意思是要关闭计算机
                // 参数 /t 0 的意思是告诉计算机 0 秒之后执行命令
                Process.Start("shutdown", "/s /t 0");
            }
    
            [DllImport("user32")]
            public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
            /// <summary>
            /// 注销
            /// </summary>
            public static void ExitWindows()
            {
                ExitWindowsEx(0, 0);
            }
            [DllImport("user32")]
            public static extern void LockWorkStation();
            /// <summary>
            /// 锁定
            /// </summary>
            public static void LockWindows()
            {
                LockWorkStation();
            }
            [DllImport("PowrProf.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);
            /// <summary>
            /// 睡眠
            /// </summary>
            public static void SleepWindows()
            {
                SetSuspendState(false, true, true);
            }
            /// <summary>
            /// 休眠
            /// </summary>
            public static void dormancyWindows()
            {
                SetSuspendState(true, true, true);
            }
    
        }
    /// <summary>
            /// 程序自启动
            /// </summary>
            /// <param name="exeName">应用程序名称</param>
            /// <param name="exePath">文件路径</param>
            public static void autoRun(string exeName, string exePath)
            {
                RegistryKey runItem = Registry.CurrentUser.OpenSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun", true);
                try
                {
                    if (runItem != null)
                    {
                        runItem.SetValue(exeName, exePath);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            /// <summary>
            /// 删除自启动
            /// </summary>
            /// <param name="exeName">文件名称</param>
            public static void deleteAutoRun(string exeName)
            {
                RegistryKey runItem = Registry.CurrentUser.OpenSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun", true);
                try
                {
                    if (runItem != null)
                    {
                        runItem.DeleteSubKey(exeName);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
  • 相关阅读:
    170322操作系统定义、功能、位置与历史
    java 多线程
    java 网络编程TCP程序设计
    java 事件处理机制
    java GUI编程
    170321php3第4章 PHP5的基本语法+作业九九乘法表
    170320网络编程 udpclient udpGroupClient
    二叉树线索化
    哈希表
    二叉搜索树
  • 原文地址:https://www.cnblogs.com/Zingu/p/14713735.html
Copyright © 2011-2022 走看看