WMI函数使用简介(一)

1、 什么是WMI

WMI是英文Windows Management Instrumentation的简写,它的功能主要是:访问本地主机的一些信息和服务,可以管理远程计算机(当然你必须要拥有足够的权限),比如:重启,关机,关闭进程,创建进程等。


2、 使用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;

 }

}

在使用上面函数时,需要加入System.Manager.dll

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、 使用WMI列出机器中所有的共享资源

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));
  }
 }

}

5、 使用WMI控制让系统中的某个文件夹共享或取消共享

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 实现的共享",""};
同时需要注意登陆用户的权限


6、 使用WMI获得系统服务的运行状态

private void getServices()
{
 ManagementObjectCollection queryCollection;
 string[] lvData =  new string[4];
  
 try
 {
  queryCollection = getServiceCollection("SELECT * FROM Win32_Service");
  foreach ( ManagementObject mo in queryCollection)
  {
   //create child node for operating system
   lvData[0] = mo["Name"].ToString();
   lvData[1] = mo["StartMode"].ToString();
   if (mo["Started"].Equals(true))
    lvData[2] = "Started";
   else
    lvData[2] = "Stop";
    lvData[3] = mo["StartName"].ToString();
     
    //create list item
    ListViewItem lvItem = new ListViewItem(lvData,0);
    listViewServices.Items.Add(lvItem);
  }
 }
 catch (Exception e)
 {
  MessageBox.Show("Error: " + e);
 }
}

7、 通过WMI修改IP,而实现不用重新启动

using System;
using System.Management;
using System.Threading;

namespace WmiIpChanger
{
 class IpChanger
 {
  [MTAThread]
  static void Main(string[] args)
  {
   ReportIP();
   // SwitchToDHCP();
   SwitchToStatic();
   Thread.Sleep( 5000 );
   ReportIP();
   Console.WriteLine( "end." );
  }

  static void SwitchToDHCP()
  {
   ManagementBaseObject inPar = null;
   ManagementBaseObject outPar = null;
   ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
   ManagementObjectCollection moc = mc.GetInstances();
   foreach( ManagementObject mo in moc )
   {
    if( ! (bool) mo["IPEnabled"] )
     continue;

    inPar = mo.GetMethodParameters("EnableDHCP");
    outPar = mo.InvokeMethod( "EnableDHCP", inPar, null );
    break;
   }
  }

  static void SwitchToStatic()
  {
   ManagementBaseObject inPar = null;
   ManagementBaseObject outPar = null;
   ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
   ManagementObjectCollection moc = mc.GetInstances();
   foreach( ManagementObject mo in moc )
   {
    if( ! (bool) mo[ "IPEnabled" ] )
     continue;

    inPar = mo.GetMethodParameters( "EnableStatic" );
    inPar["IPAddress"] = new string[] { "192.168.1.1" };
    inPar["SubnetMask"] = new string[] { "255.255.255.0" };
    outPar = mo.InvokeMethod( "EnableStatic", inPar, null );
    break;
   }
  }

  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( "{0}\n SVC: '{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(string sad in addresses)
     Console.WriteLine( "\t'{0}'", sad );

    Console.WriteLine( " Subnets :" );
    foreach(string sub in subnets )
     Console.WriteLine( "\t'{0}'", sub );
   }
  }
 }
}


你可能感兴趣的:(exception,String,object,null,Class,disk)