走,捡灌灌去

博客园 首页 联系 订阅 管理

啥也不多说,直接上码: 

 1 //远程重启方法    
 2 public static bool Shutdown(ManagementScope scope)
 3 {
 4     ObjectQuery query=new ObjectQuery("select * from Win32_OperatingSystem");
 5     using(var searcher=new ManagementObjectSearcher(scope,query))
 6     {
 7         ManagementObjectCollection operates=searcher.Get();
 8         foreach(ManagementObject item in operates)
 9         {
10             item.InvokeMethod("Reboot",str);
11         }
12     }
13     return true;
14 }
View Code

 

  1 首先引用动态库AxMSTSCLib.dll跟MSTSCLib.dll。
  2 
  3 using system.Management;
  4 using system.net.Sockets;
  5 using system.net;
  6 using system.Diagnostics;
  8 namespace WMI
  9 {
   public Class WMITools
10
{ 11 12   //远程开机方法 13   public static void WakeUp(string macAddress) 14   { 15 16     if(string.IsNullOrWhiteSpace(macAddress)) 17 return; 18     byte[] mac =GetComputerMac(macAddress); 19     WakeUp(mac); 20   } 21 22   //远程开机,网卡需要具备远程唤醒功能 23   private static void WakeUp(byte[] mac) 24   { 25     UdpClient client =new UdpClient(); 26     client.Connect(IPAddress.Broadcast,9090); 27     byte[] packet=new byte[17*6]; 28     for(int i=0;i<6;i++) 29     { 30 packet[i]=0xFF; 31 } 32     for(int i=1;i<16;i++) 33     { 34       for(int j=0;j<6;j++) 35       { 36 packet[i*6+j]=mac[j]; 37 } 38     } 39     int result =client.Send(packet,packet.Length); 40     client.Close(); 41   } 42 43   //返回MAC值 44   private static byte[] GetComputerMac(string macStr) 45   { 46     byte[] mac =new byte[6]; 47     string[] str=macStr.Split(':','-'); 48     for(int i=0;i<str.Length;i++) 49     { 50       mac[i]=Convert.ToByte(str[i],16); 51     } 52     return mac; 53   } 54 55   public static ManagementScope CreateManagementScope(string server,string userName,string pwd) 56   { 57     string serverString =@"\\"+server+@"\root\cimv2"; 58     ManagementScope scope=new ManagementScope(serverString); 59     scope.Options =new ConnectionOptions 60     { 61         UserName=userName, 62         Password=pwd, 63         Impersonation=ImpersonationLevel.Impersonate, 64         Authentication=AuthenticationLevel.PacketPrivacy 65     }; 66     return scope; 67   } 68 69   //远程关机方法 70 71   public static bool Shutdown(ManagementScope scope) 72   { 73     ObjectQuery query=new ObjectQuery("select * from Win32_OperatingSystem"); 74     using(var searcher=new ManagementObjectSearcher(scope,query)) 75     { 76       ManagementObjectCollection operates=searcher.Get(); 77       foreach(ManagementObject item in operates) 78       { 79         string[] str ={""}; 80         item.InvokeMethod("Shutdown",str); 81       } 82     } 83     return true; 84   } 85 86   //远程关软件方法 87 88   public static bool CloseSoft(ManagementScope scope,string processName) 89   { 90     SelectQuery query =new SelectQuery("select * from Win32_Process where Name='"+processName+"'"); 91     using (var searcher=new ManagementObjectSearcher(scope,query)) 92     { 93       foreach(ManagementObject item in searcher.Get()) 94       { 95         if(item["Name"].ToString()==processName) 96         { 97           item.InvokeMethod("Terminate",null); 98         } 99       } 100     } 101     return true; 102   } 103 }
104 }

 

posted on 2015-12-04 14:44  走,捡灌灌去  阅读(1151)  评论(0编辑  收藏  举报