打开右边的“解决方案资源管理器”--展开树结构--引用--添加引用---在弹出的对话框中找到System.Management---双击添加它---确定,即建成对System.Management.dll的引用。然后在程序的开头再写入
using System.Management;
using System.Runtime.InteropServices; //加入此名字间是为了引用Windows API来实现关机
程序如下:
-------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
using System.Runtime.InteropServices; //加入此名字间是为了引用Windows API来实现关机
obj = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach(ManagementObject oc in obj.Get())
{
DialogResult result = MessageBox.Show("Are you sure want to reboot?","Confirm",MessageBoxButtons.OKCancel,MessageBoxIcon.Information); //这里为确定用户是否点击“确定”按钮
if(DialogResult.OK == result)
{
oc.InvokeMethod("Reboot",null);//点击“确定”按钮后就调用此方法重启机器
}