110,537
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace CloseComputer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void CloseComputer(string strname, string strpwd, string ip, string doinfo)
{
ConnectionOptions op = new ConnectionOptions();
op.Username = strname;//连接电脑用户名 我不明白这个用户名 是本机计算机名称 还是远程计算机名称 怎么获取
op.Password = doinfo;//连接电脑密码
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", op);
try
{
scope.Connect();
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
//得到WMI控制
ManagementObjectCollection queryCollection1 = query1.Get();
foreach (ManagementObject mobj in queryCollection1)
{
string[] str ={ "" };
mobj.InvokeMethod(doinfo, str);
}
MessageBox.Show("操作成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
CloseComputer(this.textBox2.Text.Trim(), this.textBox3.Text.Trim(), this.textBox1.Text.Trim(), "Shutdown");
}
}
}