方法一:
public void Shutdown(string str, string ip)
{
//定义连接远程计算机的一些选项
ConnectionOptions options = new ConnectionOptions();
options.Username = "administrator";
options.Password = "1qa";
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", options);
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass(scope,new ManagementPath("Win32_OperatingSystem"),null);
mcWin32.Get();
// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
// Flag 1 means we want to shut down the system
mboShutdownParams["Flags"] = "5";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
}
}
摘自:https://blog.csdn.net/lovegod12/article/details/3950240
WMI中Win32_OperationSystem的方法Win32ShutDown(flag)中flag的参数可以是下表中的任意一种:
值 描述
0 注销
0 + 4 强制注销
1 关机
1 + 4 强制关机
2 重起
2 + 4 强制重起
8 关闭电源
8 + 4 强制关闭电源
下面是示例:
//关闭计算机
private void btn_Shutdown_Click(object sender, EventArgs e)
{
string IPShutdown = "192.168.1.100";
DialogResult dlResult = MessageBox.Show("确实要关闭“" + IPShutdown + "”电源吗?", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlResult == DialogResult.Yes)
{
string[] inParams ={ "8", "4" };
BootComputer ShutdownBootComputer = new BootComputer();
ShutdownBootComputer.strIp = IPShutdown;
ShutdownBootComputer.strAdmin = txtAdmin.Text.Trim();
ShutdownBootComputer.strPassword = txtPassword.Text.Trim();
ShutdownBootComputer.strMothod = "Win32Shutdown";
ShutdownBootComputer.inParams = inParams;
ShutdownBootComputer.BootMachine();
}
}
//关闭重启计算机(支持多线程)
public class BootComputer
{
public string strIp, strAdmin, strPassword, strMothod;
public string[] inParams;
public void BootMachine()
{
ConnectionOptions BootConn = new ConnectionOptions();
BootConn.Username = strAdmin;
BootConn.Password = strPassword;
ManagementScope ms = new ManagementScope("" + strIp + "//root//cimv2", BootConn);
ms.Options.EnablePrivileges = true;
if (!string.IsNullOrEmpty(strAdmin) && !string.IsNullOrEmpty(strPassword))
{
try { ms.Connect(); }
catch { }
}
if (ms.IsConnected)
{
try
{
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq);
ManagementObjectCollection moc = mos.Get();
foreach (ManagementObject mo in moc)
{
string[] ss = inParams;
mo.InvokeMethod(strMothod, ss);
}
}
catch (Exception ex)
{
MessageBox.Show(strIp + ":" + ex.Message + "网络不通或用户名、密码不正确!");
}
}
}
}
方法二:
static void ShutDown(string str, string ip)
{
//定义连接远程计算机的一些选项
ConnectionOptions options = new ConnectionOptions();
options.Username = "administrator";
options.Password = "1qa";
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", options);
try
{
//用给定管理者用户名和口令连接远程的计算机
scope.Connect();
ObjectQuery oq = new ObjectQuery("select * from win32_OperatingSystem");
ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection1 = query1.Get();
foreach (ManagementObject mo in queryCollection1)
{
string[] ss = { "" };
if (str == "重新启动")
{
mo.InvokeMethod("Reboot", ss);
}
if (str == "关闭计算机")
{
mo.InvokeMethod("Shutdown", ss);
}
}
}
catch (Exception er)
{
Console.WriteLine("连接" + ip + "出错,出错信息为:" + er.Message);
}
}
方法三:此方法能实现异地定时关机
Process p = new Process();//实例化一个独立进程
p.StartInfo.FileName = "cmd.exe";//进程打开的文件为Cmd
p.StartInfo.UseShellExecute = false;//是否启动系统外壳选否
p.StartInfo.RedirectStandardInput = true;//这是是否从StandardInput输入
p.StartInfo.CreateNoWindow = true;//这里是启动程序是否显示窗体
p.Start();//启动
p.StandardInput.WriteLine(@"net use \\192.168.1.244\ipc$ ""1qa"" /user:""administrator""");//运行关机命令shutdown (-s)是关机 (-t)是延迟的时间 这里用秒计算 10就是10秒后关机
p.StandardInput.WriteLine(@"shutdown -s -t 60 -m \\192.168.1.244");
p.StandardInput.WriteLine(@"net use \\192.168.1.244 /delete");
p.StandardInput.WriteLine("exit");//退出cmd 此非原创,来源于网络,希望您满意,谢谢!
p.Dispose();
p.Close();