C# 设置注销、关闭、重新启动计算机

        //设置注销、关闭、重新启动计算机参数
        [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
        private static extern int ExitWindowsEx(int uFlags, int dwReserved);

        //注销计算机
        public void logout()
        {
            DialogResult dr = MessageBox.Show("客户请求注销计算机,确定注销吗?", "注销计算机", MessageBoxButtons.OKCancel);

            if (dr == DialogResult.OK)//如果点击“确定”按钮
            {
                ExitWindowsEx(0, 0);
            }
        }
        //关闭计算机
        public void closepc()
        {
            //创建访问控制本地系统进程的对象实例
            DialogResult dr = MessageBox.Show("客户请求关闭计算机,确定关闭计算机吗?", "关闭计算机", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)//如果点击“确定”按钮
            {
                System.Diagnostics.Process myprocess = new System.Diagnostics.Process();
                myprocess.StartInfo.FileName = "cmd.exe";
                myprocess.StartInfo.UseShellExecute = false;
                myprocess.StartInfo.RedirectStandardInput = true;
                myprocess.StartInfo.RedirectStandardOutput = true;
                myprocess.StartInfo.RedirectStandardError = true;
                myprocess.StartInfo.CreateNoWindow = true;
                myprocess.Start();
                myprocess.StandardInput.WriteLine("shutdown -s -t 0");
            }
        }
        //重新启动计算机
        public void afreshstartpc()
        {
            DialogResult dr = MessageBox.Show("客户请求重新启动计算机,确定重新启动计算机?", "重新启动计算机", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)//如果点击“确定”按钮
            {
                //创建访问控制本地系统进程的对象实例
                System.Diagnostics.Process myprocess = new System.Diagnostics.Process();
                myprocess.StartInfo.FileName = "cmd.exe";
                myprocess.StartInfo.UseShellExecute = false;
                myprocess.StartInfo.RedirectStandardInput = true;
                myprocess.StartInfo.RedirectStandardOutput = true;
                myprocess.StartInfo.RedirectStandardError = true;
                myprocess.StartInfo.CreateNoWindow = true;
                myprocess.Start();
                myprocess.StandardInput.WriteLine("shutdown -r -t 0");
            }
        }

 

转载于:https://www.cnblogs.com/pmaojie/archive/2013/02/18/2915330.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值