首先添加對 System.Management的引用
然後代碼入下:
ConnectionOptions op = new ConnectionOptions ( ) ;
op.Username ="wgscd";’或者你的帳號(注意要有管理員的權限)
op.PassWord = "wgscd" ; ’你的密碼
ManagementScope scope = new ManagementScope( "\\\\" + "106.54.20.1" + "\\root\\wgscd",
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 ( "Reboot" , str ) ;
}
}
catch
{
}