很多的朋友對WMI可能見過但理解不深,我也是十分想了解關於WMI的知識,可一直找不對太合適的資料,在網上的一些資料不是有很多錯誤,就是講解不清,我有空的時候將關於WMI的知識集中一下,放在這裡便於大家學習。本貼會不斷增加。
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 實現的共享",""};
就可以實現授權給最多用戶了。
6、 如何獲得系統服務的運行狀態?
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 )