try
{
//设定生成的WMI所需的所有设置
ConnectionOptions co = new ConnectionOptions();
co.Username = "Administrator";//用户名(必须是管理员才能有权限实现关机重启等操作)
co.Password = "aa";//密码
string ls_tmp = "grd1-hollis-yao";//远程(本地)机器名或IP地址 //设定用于执行WMI操作的范围
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + ls_tmp + "\\root\\cimv2", co);
//连接到实际操作的WMI范围
ms.Connect();
//设定通过WMI要查询的内容
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher query = new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection queryCollection = query.Get();
//通过对产生的WMI的实例集合进行检索,执行关机命令
foreach( ManagementObject mo in queryCollection )
{
string[] ss={""};
mo.InvokeMethod("ShutDown",ss); //将ShutDown替换成Reboot就是重启了
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}执行到ms.Connect()发生异常,异常信息即ex.Message = "无法为本地连结使用用户凭证\r\n"。用户名,密码,机器名都没有错误啊!然后我换了另外一种方式,就是把
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + ls_tmp + "\\root\\cimv2", co);
改成
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + ls_tmp + "\\root\\cimv2");
大家可以看到,去掉了最后的co连接对象,因为连接本地好像可以不用用户名和密码,这种情况下运行到mo.InvokeMethod("ShutDown",ss)出现异常,异常信息即
ex.Message = "没有保留特权"。另外,一直不清楚string[] ss={""};这句应该怎么赋值,应该有哪些可以赋值的选择,另请高手贴出WMI的相关资料,越详细越好,这方面的资料太少了!
小弟,跪谢了!!!
{
//设定生成的WMI所需的所有设置
ConnectionOptions co = new ConnectionOptions();
co.Username = "Administrator";//用户名(必须是管理员才能有权限实现关机重启等操作)
co.Password = "aa";//密码
string ls_tmp = "grd1-hollis-yao";//远程(本地)机器名或IP地址 //设定用于执行WMI操作的范围
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + ls_tmp + "\\root\\cimv2", co);
//连接到实际操作的WMI范围
ms.Connect();
//设定通过WMI要查询的内容
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher query = new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection queryCollection = query.Get();
//通过对产生的WMI的实例集合进行检索,执行关机命令
foreach( ManagementObject mo in queryCollection )
{
string[] ss={""};
mo.InvokeMethod("ShutDown",ss); //将ShutDown替换成Reboot就是重启了
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}执行到ms.Connect()发生异常,异常信息即ex.Message = "无法为本地连结使用用户凭证\r\n"。用户名,密码,机器名都没有错误啊!然后我换了另外一种方式,就是把
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + ls_tmp + "\\root\\cimv2", co);
改成
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + ls_tmp + "\\root\\cimv2");
大家可以看到,去掉了最后的co连接对象,因为连接本地好像可以不用用户名和密码,这种情况下运行到mo.InvokeMethod("ShutDown",ss)出现异常,异常信息即
ex.Message = "没有保留特权"。另外,一直不清楚string[] ss={""};这句应该怎么赋值,应该有哪些可以赋值的选择,另请高手贴出WMI的相关资料,越详细越好,这方面的资料太少了!
小弟,跪谢了!!!
password)
{
ManagementScope sc = null;
ConnectionOptions co = null;
ObjectQuery oq = null;
ManagementObjectSearcher os = null; try
{
co = new ConnectionOptions();
co.Impersonation = ImpersonationLevel.Impersonate;
co.EnablePrivileges = true;
if (machineName.ToUpper() == Environment.MachineName.ToUpper() )
sc = new ManagementScope(@"\ROOT\CIMV2", co);
else
{
co.Username = username;
co.Password = password;
sc = new ManagementScope(@"\\" + machineName + @"\ROOT\CIMV2", co);
}
sc.Connect();
oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
os = new ManagementObjectSearcher(sc, oq); foreach( ManagementObject operatingSystem in os.Get())
{
ManagementBaseObject outParams = operatingSystem.InvokeMethod ("Shutdown",
null, null);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
os.Dispose();
}
}
关机之后,就停在了“您可以安全关闭计算机了”这个画面了,必须手动才能切断电源。
我这个是ATX电源啊,应该不用手动关闭的啊,为什么呢??还有,在代码中:
co.Impersonation = ImpersonationLevel.Impersonate; 可以不用设置,这是默认值
co.EnablePrivileges = true;如果要实现关机重启等特权操作,这个属性一定要设置,否则不能关机重启。异常提示为:“没有保留特权”。
机器的BIOS支持ACPI吧?
ManagementBaseObject outParams = operatingSystem.InvokeMethod ("Shutdown",null, null);
改成
ManagementBaseObject outParams = operatingSystem.InvokeMethod ("Win32Shutdown",8);
就能够自动关闭电源了。