1、 什么是WMI WMI是英文Windows Management Instrumentation的简写,它的功能主要是:访问本地主机的一些信息和服务,可以管理远程计算机(当然你必须要拥有足够的权限),比如:重启,关机,关闭进程,创建进程等。 2、 如何用WMI获得本地磁盘的信息? 首先要在VS.NET中创建一个项目,然后在添加引用中引用一个.net的装配件:System.Management.dll,这样你的项目才能使用WMI。代码如下: using System; using System.Management;
class Sample_ManagementObject { public static int Main(string[] args) { SelectQuery query=new SelectQuery("Select * From Win32_LogicalDisk"); ManagementObjectSearcher searcher=new ManagementObjectSearcher(query); foreach(ManagementBaseObject disk in searcher.Get()) { Console.WriteLine("/r/n"+disk["Name"] +" "+disk["DriveType"] + " " + disk["VolumeName"]); }
Console.ReadLine();
return 0;
}
}
disk["DriveType"] 的返回值意义如下:
1 No type 2 Floppy disk 3 Hard disk 4 Removable drive or network drive 5 CD-ROM 6 RAM disk
3、如何用WMI获得指定磁盘的容量? using System; using System.Management;
// This example demonstrates reading a property of a ManagementObject. class Sample_ManagementObject { public static int Main(string[] args) { ManagementObject disk = new ManagementObject( "win32_logicaldisk.deviceid=/"c:/""); disk.Get(); Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes"); Console.ReadLine(); return 0; } }
4、 如何列出机器中所有的共享资源? using System; using System.Management;
class TestApp { [STAThread] static void Main() { ManagementObjectSearcher searcher = new ManagementObjectSearcher( "SELECT * FROM Win32_share"); foreach (ManagementObject share in searcher.Get()) { Console.WriteLine(share.GetText(TextFormat.Mof)); } }
} 别忘记在引用中把System.Management添加进来。
5、 怎样写程控制让系统中的某个文件夹共享或取消共享.? 首先,这需要以有相应权限的用户登录系统才行。然后,试试下面的代码: using System; using System.Management;
class CreateShare { public static void Main(string[] args) { ManagementClass _class = new ManagementClass(new ManagementPath("Win32_Share"));
object[] obj = {"C://Temp","我的共享",0,10,"Dot Net 实现的共享",""};
_class.InvokeMethod("create",obj); } } 将 object[] obj = {"C://Temp","我的共享",0,10,"Dot Net 实现的共享",""}; 改为 object[] obj = {"C://Temp","我的共享",0,null,"Dot Net 实现的共享",""}; 就可以实现授权给最多用户了。
static void ReportIP() { Console.WriteLine( "****** Current IP addresses:" ); ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach( ManagementObject mo in moc ) { if( ! (bool) mo[ "IPEnabled" ] ) continue;
Console.WriteLine( " Addresses :" ); foreach(string sad in addresses) Console.WriteLine( "/t'{0}'", sad );
Console.WriteLine( " Subnets :" ); foreach(string sub in subnets ) Console.WriteLine( "/t'{0}'", sub ); } } } }
8、 如何利用WMI远程重启远程计算机? using System; using System.Management; namespace WMI3 { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { static void Main(string[] args) { Console.WriteLine("Computer details retrieved using Windows Management Instrumentation (WMI)"); Console.WriteLine("mailto:Written%2002/01/02%20By%20John%20O'Donnell%20-%20csharpconsulting@hotmail.com"); Console.WriteLine("======================================== ================================="); //连接远程计算机 ConnectionOptions co = new ConnectionOptions(); co.Username = "john"; co.Password = "john"; System.Management.ManagementScope ms = new System.Management.ManagementScope("////192.168.1.2//root//cimv2", co); //查询远程计算机 System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms,oq); ManagementObjectCollection queryCollection1 = query1.Get(); foreach( ManagementObject mo in queryCollection1 ) { string[] ss={""}; mo.InvokeMethod("Reboot",ss); Console.WriteLine(mo.ToString()); } } } }
9、 利用WMI创建一个新的进程? using System; using System.Management;
// This sample demonstrates invoking a WMI method using parameter objects public class InvokeMethod { public static void Main() {
//Get the object on which the method will be invoked ManagementClass processClass = new ManagementClass("Win32_Process");
//Get an input parameters object for this method ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
//Fill in input parameter values inParams["CommandLine"] = "calc.exe";
//Execute the method ManagementBaseObject outParams = processClass.InvokeMethod ("Create", inParams, null);
//Display results //Note: The return code of the method is provided in the "returnvalue" property of the outParams object Console.WriteLine("Creation of calculator process returned: " + outParams["returnvalue"]); Console.WriteLine("Process ID: " + outParams["processId"]); } }
10、 如何通过WMI终止一个进程? using System; using System.Management;
// This example demonstrates how to stop a system service. class Sample_InvokeMethodOptions { public static int Main(string[] args) { ManagementObject service = new ManagementObject("win32_service=/"winmgmt/""); InvokeMethodOptions options = new InvokeMethodOptions(); options.Timeout = new TimeSpan(0,0,0,5);
SELECT MACAddress FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))
结果:08:00:46:63:FF:8C
CPU的系列号
Select ProcessorId From Win32_Processor
结果:3FEBF9FF00000F24
主板的系列号
Select SerialNumber From Win32_BIOS
结果:28362630-3700521 获取硬盘ID String HDid; ManagementClass cimobject = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc = cimobject.GetInstances(); foreach(ManagementObject mo in moc) { HDid = (string)mo.Properties["Model"].value;
MessageBox.Show(HDid ); }
14、 一个使用WMI后的异常处理的问题 下面是我整理的一段代码.
ManagementObjectCollection queryCollection1; ConnectionOptions co = new ConnectionOptions(); co.Username = "administrator"; co.Password = "111"; try { System.Management.ManagementScope ms = new System.Management.ManagementScope(@"//csnt3/root/cimv2", co); System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem"); ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms,oq);
queryCollection1 = query1.Get(); foreach( ManagementObject mo in queryCollection1 ) { string[] ss={""}; mo.InvokeMethod("Reboot",ss); Console.WriteLine(mo.ToString()); } } catch(Exception ee) {