WMI使用技巧集

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第1页共22页WMI使用技巧集很多的朋友对WMI可能见过但理解不深,我也是十分想了解关于WMI的知识,可一直找不对太合适的资料,在网上的一些资料不是有很多错误,就是讲解不清,我有空的时候将关于WMI的知识集中一下,放在这里便于大家学习。本贴会不断增加。1、什么是WMIWMI是英文WindowsManagementInstrumentation的简写,它的功能主要是:访问本地主机的一些信息和服务,可以管理远程计算机(当然你必须要拥有足够的权限),比如:重启,关机,关闭进程,创建进程等。2、如何用WMI获得本地磁盘的信息?首先要在VS.NET中创建一个项目,然后在添加引用中引用一个.net的装配件:System.Management.dll,这样你的项目才能使用WMI。代码如下:usingSystem;usingSystem.Management;classSample_ManagementObject{publicstaticintMain(string[]args){SelectQueryquery=newSelectQuery(Select*FromWin32_LogicalDisk);ManagementObjectSearchersearcher=newManagementObjectSearcher(query);foreach(ManagementBaseObjectdiskinsearcher.Get()){Console.WriteLine(\r\n+disk[Name]++disk[DriveType]++disk[VolumeName]);}第2页共22页Console.ReadLine();return0;}}disk[DriveType]的返回值意义如下1Notype2Floppydisk3Harddisk4Removabledriveornetworkdrive5CD-ROM6RAMdisk3、如何用WMI获得指定磁盘的容量?usingSystem;usingSystem.Management;//ThisexampledemonstratesreadingapropertyofaManagementObject.classSample_ManagementObject{publicstaticintMain(string[]args){第3页共22页ManagementObjectdisk=newManagementObject(win32_logicaldisk.deviceid=\c:\);disk.Get();Console.WriteLine(LogicalDiskSize=+disk[Size]+bytes);Console.ReadLine();return0;}}4、如何列出机器中所有的共享资源?usingSystem;usingSystem.Management;classTestApp{[STAThread]staticvoidMain(){ManagementObjectSearchersearcher=newManagementObjectSearcher(SELECT*FROMWin32_share);foreach(ManagementObjectshareinsearcher.Get()){Console.WriteLine(share.GetText(TextFormat.Mof));}}第4页共22页}别忘记在引用中把System.Management添加进来。5、怎样写程控制让系统中的某个文件夹共享或取消共享.?首先,这需要以有相应权限的用户登录系统才行。然后,试试下面的代码:usingSystem;usingSystem.Management;classCreateShare{publicstaticvoidMain(string[]args){ManagementClass_class=newManagementClass(newManagementPath(Win32_Share));object[]obj={C:\\Temp,我的共享,0,10,DotNet实现的共享,};_class.InvokeMethod(create,obj);}}将object[]obj={C:\\Temp,我的共享,0,10,DotNet实现的共享,};改为object[]obj={C:\\Temp,我的共享,0,null,DotNet实现的共享,};就可以实现授权给最多用户了。第5页共22页6、如何获得系统服务的运行状态?privatevoidgetServices(){ManagementObjectCollectionqueryCollection;string[]lvData=newstring[4];try{queryCollection=getServiceCollection(SELECT*FROMWin32_Service);foreach(ManagementObjectmoinqueryCollection){//createchildnodeforoperatingsystemlvData[0]=mo[Name].ToString();lvData[1]=mo[StartMode].ToString();if(mo[Started].Equals(true))lvData[2]=Started;elselvData[2]=Stop;lvData[3]=mo[StartName].ToString();//createlistitemListViewItemlvItem=newListViewItem(lvData,0);listViewServices.Items.Add(lvItem);}第6页共22页}catch(Exceptione){MessageBox.Show(Error:+e);}}7、通过WMI修改IP,而实现不用重新启动?usingSystem;usingSystem.Management;usingSystem.Threading;namespaceWmiIpChanger{classIpChanger{[MTAThread]staticvoidMain(string[]args){ReportIP();//SwitchToDHCP();SwitchToStatic();Thread.Sleep(5000);ReportIP();第7页共22页Console.WriteLine(end.);}staticvoidSwitchToDHCP(){ManagementBaseObjectinPar=null;ManagementBaseObjectoutPar=null;ManagementClassmc=newManagementClass(Win32_NetworkAdapterConfiguration);ManagementObjectCollectionmoc=mc.GetInstances();foreach(ManagementObjectmoinmoc){if(!(bool)mo[IPEnabled])continue;inPar=mo.GetMethodParameters(EnableDHCP);outPar=mo.InvokeMethod(EnableDHCP,inPar,null);break;}}staticvoidSwitchToStatic(){ManagementBaseObjectinPar=null;ManagementBaseObjectoutPar=null;ManagementClassmc=new第8页共22页ManagementClass(Win32_NetworkAdapterConfiguration);ManagementObjectCollectionmoc=mc.GetInstances();foreach(ManagementObjectmoinmoc){if(!(bool)mo[IPEnabled])continue;inPar=mo.GetMethodParameters(EnableStatic);inPar[IPAddress]=newstring[]{192.168.1.1};inPar[SubnetMask]=newstring[]{255.255.255.0};outPar=mo.InvokeMethod(EnableStatic,inPar,null);break;}}staticvoidReportIP(){Console.WriteLine(******CurrentIPaddresses:);ManagementClassmc=newManagementClass(Win32_NetworkAdapterConfiguration);ManagementObjectCollectionmoc=mc.GetInstances();foreach(ManagementObjectmoinmoc){if(!(bool)mo[IPEnabled])continue;第9页共22页Console.WriteLine({0}\nSVC:'{1}'MAC:[{2}],(string)mo[Caption],(string)mo[ServiceName],(string)mo[MACAddress]);string[]addresses=(string[])mo[IPAddress];string[]subnets=(string[])mo[IPSubnet];Console.WriteLine(Addresses:);foreach(stringsadinaddresses)Console.WriteLine(\t'{0}',sad);Console.WriteLine(Subnets:);foreach(stringsubinsubnets)Console.WriteLine(\t'{0}',sub);}}}}8、如何利用WMI远程重启远程计算机?usingSystem;usingSystem.Management;namespaceWMI3{///summary///SummarydescriptionforClass1.////summary第10页共22页classClass1{staticvoidMain(string[]args){Console.WriteLine(ComputerdetailsretrievedusingWindowsManagementInstrumentation(WMI));Console.WriteLine(mailto:Written%2002/01/02%20By%20John%20O'Donnell%20-%20csharpconsulting@hotmail.com);Console.WriteLine(=========================================================================);//连接远程计算机ConnectionOptionsco=newConnectionOptions();co.Username=john;co.Password=john;System.Management.ManagementScopems=newSystem.Management.ManagementScope(\\\\192.168.1.2\\root\\cimv2,co);//查询远程计算机System.Management.ObjectQueryoq=newSystem.Management.ObjectQuery(SELECT*FROMWin32_OperatingSystem);ManagementObjectSearcherquery1=newM

1 / 22
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功