zoukankan      html  css  js  c++  java
  • 注销、关闭和重启计算机

    源码如下:

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    namespace 关机重启
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
            private static extern int ExitWindowsEx(int uFlags,int dwReserved);
            /// <summary>
            /// 注销计算机
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                ExitWindowsEx(0, 0);
            }
            /// <summary>
            /// 关闭计算机
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                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.Start();
                myProcess.StandardInput.WriteLine("shutdown -s -t 0");
            }
            /// <summary>
            /// 重启计算机
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button3_Click(object sender, EventArgs e)
            {
                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");
            }
        }
    }
    以上是打开cmd.exe,然后写命令来实现关机等功能

    感谢来访,共同学习!
  • 相关阅读:
    [函數] Firemonkey Android 取得系统参数设定的字型大小
    [示例] 访问类的私有属性
    [修正] 移动平台曲线不平滑的问题(如:TRectangle, TPath...等)
    [修正] Firemonkey TSelection 控件等比缩放时,左下角拉动问题
    [修正] iOS 10 使用相机及相簿闪退的问题修正
    报表之表头
    报表字段刷新
    tfs解除锁
    sql server中排名的问题
    TFS查询无法在Excel中打开
  • 原文地址:https://www.cnblogs.com/dingxiaowei/p/3058770.html
Copyright © 2011-2022 走看看