WMI使用集锦

WMI使用集锦

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

WMI使用集锦using System.Management;
WMI使用集锦
using System.Threading; 
3.示例代码
3.1 html代码
WMI使用集锦<HTML>
WMI使用集锦    
<HEAD>
WMI使用集锦        
<title>WMITest</title>
WMI使用集锦        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
WMI使用集锦        
<meta name="CODE_LANGUAGE" Content="C#">
WMI使用集锦        
<meta name="vs_defaultClientScript" content="JavaScript">
WMI使用集锦        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
WMI使用集锦    
</HEAD>
WMI使用集锦    
<body MS_POSITIONING="GridLayout">
WMI使用集锦        
<form id="Form1" method="post" runat="server">
WMI使用集锦            
<FONT face="宋体">
WMI使用集锦                
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 16px" runat="server"
WMI使用集锦                    Text
="获得本地磁盘的信息" Width="152px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 56px" runat="server"
WMI使用集锦                    Text
="获得指定磁盘的容量" Width="144px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button3" style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute; TOP: 96px" runat="server"
WMI使用集锦                    Text
="列出机器中所有的共享资源" Width="192px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button4" style="Z-INDEX: 104; LEFT: 8px; POSITION: absolute; TOP: 128px" runat="server"
WMI使用集锦                    Text
="控制让系统中的某个文件夹共享或取消共享" Width="264px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button5" style="Z-INDEX: 105; LEFT: 48px; POSITION: absolute; TOP: 160px" runat="server"
WMI使用集锦                    Text
="获得系统服务的运行状态"></asp:Button>
WMI使用集锦                
<asp:Button id="Button6" style="Z-INDEX: 106; LEFT: 40px; POSITION: absolute; TOP: 192px" runat="server"
WMI使用集锦                    Text
="通过WMI修改IP,而实现不用重新启动" Width="248px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button7" style="Z-INDEX: 107; LEFT: 24px; POSITION: absolute; TOP: 224px" runat="server"
WMI使用集锦                    Text
="如何利用WMI远程重启远程计算机" Width="232px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button8" style="Z-INDEX: 108; LEFT: 40px; POSITION: absolute; TOP: 264px" runat="server"
WMI使用集锦                    Text
="利用WMI创建一个新的进程" Width="192px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button9" style="Z-INDEX: 109; LEFT: 32px; POSITION: absolute; TOP: 296px" runat="server"
WMI使用集锦                    Text
="如何通过WMI终止一个进程" Width="192px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button10" style="Z-INDEX: 110; LEFT: 32px; POSITION: absolute; TOP: 328px" runat="server"
WMI使用集锦                    Text
="如何用WMI 来获取远程机器的目录以及文件" Width="280px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button11" style="Z-INDEX: 111; LEFT: 424px; POSITION: absolute; TOP: 24px" runat="server"
WMI使用集锦                    Text
="网卡的MAC地址" Width="120px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button12" style="Z-INDEX: 112; LEFT: 456px; POSITION: absolute; TOP: 64px" runat="server"
WMI使用集锦                    Text
="CPU的系列号 " Width="104px"></asp:Button>
WMI使用集锦                
<asp:Button id="Button13" style="Z-INDEX: 113; LEFT: 448px; POSITION: absolute; TOP: 120px"
WMI使用集锦                    runat
="server" Text="主板的系列号"></asp:Button>
WMI使用集锦                
<asp:Button id="Button14" style="Z-INDEX: 114; LEFT: 472px; POSITION: absolute; TOP: 160px"
WMI使用集锦                    runat
="server" Text="获取硬盘ID"></asp:Button>
WMI使用集锦                
<asp:Button id="Button15" style="Z-INDEX: 115; LEFT: 448px; POSITION: absolute; TOP: 208px"
WMI使用集锦                    runat
="server" Text="获取本机的用户列表" Width="144px"></asp:Button></FONT>
WMI使用集锦        
</form>
WMI使用集锦    
</body>
WMI使用集锦
</HTML>
3.2cs代码
WMI使用集锦public class WMITest : System.Web.UI.Page
WMI使用集锦WMI使用集锦    
WMI使用集锦{
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button2;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button3;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button4;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button5;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button6;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button7;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button8;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button9;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button10;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button11;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button12;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button13;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button14;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button15;
WMI使用集锦        
protected System.Web.UI.WebControls.Button Button1;
WMI使用集锦    
WMI使用集锦        
private void Page_Load(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            
// Put user code to initialize the page here
WMI使用集锦
        }

WMI使用集锦
WMI使用集锦WMI使用集锦        
Web Form Designer generated code#region Web Form Designer generated code
WMI使用集锦        
override protected void OnInit(EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            
//
WMI使用集锦            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
WMI使用集锦            
//
WMI使用集锦
            InitializeComponent();
WMI使用集锦            
base.OnInit(e);
WMI使用集锦        }

WMI使用集锦        
WMI使用集锦WMI使用集锦        
/**//// <summary>
WMI使用集锦        
/// Required method for Designer support - do not modify
WMI使用集锦        
/// the contents of this method with the code editor.
WMI使用集锦        
/// </summary>

WMI使用集锦        private void InitializeComponent()
WMI使用集锦WMI使用集锦        
WMI使用集锦{    
WMI使用集锦            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
WMI使用集锦            
this.Button2.Click += new System.EventHandler(this.Button2_Click);
WMI使用集锦            
this.Button3.Click += new System.EventHandler(this.Button3_Click);
WMI使用集锦            
this.Button4.Click += new System.EventHandler(this.Button4_Click);
WMI使用集锦            
this.Button5.Click += new System.EventHandler(this.Button5_Click);
WMI使用集锦            
this.Button6.Click += new System.EventHandler(this.Button6_Click);
WMI使用集锦            
this.Button7.Click += new System.EventHandler(this.Button7_Click);
WMI使用集锦            
this.Button8.Click += new System.EventHandler(this.Button8_Click);
WMI使用集锦            
this.Button9.Click += new System.EventHandler(this.Button9_Click);
WMI使用集锦            
this.Button10.Click += new System.EventHandler(this.Button10_Click);
WMI使用集锦            
this.Button11.Click += new System.EventHandler(this.Button11_Click);
WMI使用集锦            
this.Button12.Click += new System.EventHandler(this.Button12_Click);
WMI使用集锦            
this.Button13.Click += new System.EventHandler(this.Button13_Click);
WMI使用集锦            
this.Button14.Click += new System.EventHandler(this.Button14_Click);
WMI使用集锦            
this.Button15.Click += new System.EventHandler(this.Button15_Click);
WMI使用集锦            
this.Load += new System.EventHandler(this.Page_Load);
WMI使用集锦
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
1.如何用WMI获得指定磁盘的容量#region 1.如何用WMI获得指定磁盘的容量
WMI使用集锦        
private string DriveType(string type)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            
string rtntype="";
WMI使用集锦            
switch (type)
WMI使用集锦WMI使用集锦            
WMI使用集锦{
WMI使用集锦                
case "1":
WMI使用集锦                    rtntype
="Not Type";
WMI使用集锦                    
break;
WMI使用集锦                
case "2":
WMI使用集锦                    rtntype
="Floppy disk";
WMI使用集锦                    
break;
WMI使用集锦                
case "3":
WMI使用集锦                    rtntype
="Hard disk";
WMI使用集锦                    
break;
WMI使用集锦                
case "4":
WMI使用集锦                    rtntype
="Removable drive or network drive";
WMI使用集锦                    
break;
WMI使用集锦                
case "5":
WMI使用集锦                    rtntype
="CD-ROM";
WMI使用集锦                    
break;
WMI使用集锦                
case "6":
WMI使用集锦                    rtntype
="RAM disk";
WMI使用集锦                    
break;
WMI使用集锦                
default :
WMI使用集锦                    
break;
WMI使用集锦            }

WMI使用集锦            
return rtntype;
WMI使用集锦        }

WMI使用集锦
WMI使用集锦        
private void Button1_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            SelectQuery query
=new SelectQuery("Select * From Win32_LogicalDisk"); 
WMI使用集锦            ManagementObjectSearcher searcher
=new ManagementObjectSearcher(query); 
WMI使用集锦            
foreach(ManagementBaseObject disk in searcher.Get()) 
WMI使用集锦WMI使用集锦            
WMI使用集锦
WMI使用集锦                Response.Write(disk[
"Name"+" "+DriveType(disk["DriveType"].ToString()) + " " + disk["VolumeName"]+"<br>"); 
WMI使用集锦            }

WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
2.如何用WMI获得指定磁盘的容量#region 2.如何用WMI获得指定磁盘的容量
WMI使用集锦        
private void Button2_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            ManagementObject disk 
= new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); 
WMI使用集锦            disk.Get(); 
WMI使用集锦            Response.Write(
"Logical Disk Size = " + disk["Size"+ " bytes");             
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
3.如何列出机器中所有的共享资源#region 3.如何列出机器中所有的共享资源
WMI使用集锦        
private void Button3_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            ManagementObjectSearcher searcher 
= new ManagementObjectSearcher("SELECT * FROM Win32_share"); 
WMI使用集锦            
foreach (ManagementObject share in searcher.Get()) 
WMI使用集锦WMI使用集锦            
WMI使用集锦
WMI使用集锦                Response.Write(share.GetText(TextFormat.Mof)); 
WMI使用集锦            }
 
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
4.怎样写程控制让系统中的某个文件夹共享或取消共享#region 4.怎样写程控制让系统中的某个文件夹共享或取消共享
WMI使用集锦        
private void Button4_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦WMI使用集锦            
/**//*首先,这需要以有相应权限的用户登录系统才行
WMI使用集锦            将 
WMI使用集锦            object[] obj = {"C:\\Temp","我的共享",0,10,"Dot Net 实现的共享",""}; 
WMI使用集锦            改为 
WMI使用集锦            object[] obj = {"C:\\Temp","我的共享",0,null,"Dot Net 实现的共享",""}; 
WMI使用集锦            就可以实现授权给最多用户了。
WMI使用集锦            
*/

WMI使用集锦            ManagementClass _class 
= new ManagementClass(new ManagementPath("Win32_Share"));
WMI使用集锦WMI使用集锦            
object[] obj = WMI使用集锦{"C:\\Temp","我的共享",0,10,"Dot Net 实现的共享",""};
WMI使用集锦            _class.InvokeMethod(
"create",obj); 
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
5.如何获得系统服务的运行状态#region 5.如何获得系统服务的运行状态
WMI使用集锦        
private void Button5_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            
string[] lvData =  new string[4];            
WMI使用集锦            ManagementObjectSearcher searcher 
=new ManagementObjectSearcher("SELECT * FROM Win32_Service"); 
WMI使用集锦            
foreach (ManagementObject mo in searcher.Get()) 
WMI使用集锦WMI使用集锦            
WMI使用集锦
WMI使用集锦                lvData[
0= mo["Name"].ToString(); 
WMI使用集锦                lvData[
1= mo["StartMode"].ToString(); 
WMI使用集锦                
if (mo["Started"].Equals(true)) 
WMI使用集锦                    lvData[
2= "Started"
WMI使用集锦                
else 
WMI使用集锦                    lvData[
2= "Stop"
WMI使用集锦                lvData[
3= mo["StartName"].ToString(); 
WMI使用集锦                Response.Write(lvData[
0]+lvData[1]+lvData[2]+lvData[3]);                    
WMI使用集锦            }
             
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
6.通过WMI修改IP,而实现不用重新启动#region 6.通过WMI修改IP,而实现不用重新启动
WMI使用集锦        
private void Button6_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            ReportIP(); 
WMI使用集锦            
// SwitchToDHCP(); 
WMI使用集锦
            SwitchToprivate(); 
WMI使用集锦            Thread.Sleep( 
5000 ); 
WMI使用集锦            ReportIP(); 
WMI使用集锦            Response.Write( 
"end." );
WMI使用集锦        }
        
WMI使用集锦        
WMI使用集锦        
private void SwitchToDHCP() 
WMI使用集锦WMI使用集锦        
WMI使用集锦
WMI使用集锦            ManagementBaseObject inPar 
= null
WMI使用集锦            ManagementBaseObject outPar 
= null
WMI使用集锦            ManagementClass mc 
= new ManagementClass("Win32_NetworkAdapterConfiguration"); 
WMI使用集锦            ManagementObjectCollection moc 
= mc.GetInstances(); 
WMI使用集锦            
foreach( ManagementObject mo in moc ) 
WMI使用集锦WMI使用集锦            
WMI使用集锦
WMI使用集锦                
if! (bool) mo["IPEnabled"] ) 
WMI使用集锦                    
continue
WMI使用集锦
WMI使用集锦                inPar 
= mo.GetMethodParameters("EnableDHCP"); 
WMI使用集锦                outPar 
= mo.InvokeMethod( "EnableDHCP", inPar, null ); 
WMI使用集锦                
break
WMI使用集锦            }
 
WMI使用集锦        }
 
WMI使用集锦
WMI使用集锦        
private void SwitchToprivate() 
WMI使用集锦WMI使用集锦        
WMI使用集锦
WMI使用集锦            ManagementBaseObject inPar 
= null
WMI使用集锦            ManagementBaseObject outPar 
= null
WMI使用集锦            ManagementClass mc 
= new ManagementClass("Win32_NetworkAdapterConfiguration"); 
WMI使用集锦            ManagementObjectCollection moc 
= mc.GetInstances(); 
WMI使用集锦            
foreach( ManagementObject mo in moc ) 
WMI使用集锦WMI使用集锦            
WMI使用集锦
WMI使用集锦                
if! (bool) mo[ "IPEnabled" ] ) 
WMI使用集锦                    
continue
WMI使用集锦
WMI使用集锦                inPar 
= mo.GetMethodParameters( "Enableprivate" ); 
WMI使用集锦WMI使用集锦                inPar[
"IPAddress"= new string[] WMI使用集锦"192.168.1.1" }
WMI使用集锦WMI使用集锦                inPar[
"SubnetMask"= new string[] WMI使用集锦"255.255.255.0" }
WMI使用集锦                outPar 
= mo.InvokeMethod( "Enableprivate", inPar, null ); 
WMI使用集锦                
break
WMI使用集锦            }
 
WMI使用集锦        }
 
WMI使用集锦
WMI使用集锦        
private void ReportIP() 
WMI使用集锦WMI使用集锦        
WMI使用集锦
WMI使用集锦            Response.Write( 
"****** Current IP addresses:" ); 
WMI使用集锦            ManagementClass mc 
= new ManagementClass("Win32_NetworkAdapterConfiguration"); 
WMI使用集锦            ManagementObjectCollection moc 
= mc.GetInstances(); 
WMI使用集锦            
foreach( ManagementObject mo in moc ) 
WMI使用集锦WMI使用集锦            
WMI使用集锦
WMI使用集锦                
if! (bool) mo[ "IPEnabled" ] ) 
WMI使用集锦                    
continue
WMI使用集锦
WMI使用集锦                
string str="{0}\n SVC: '{1}' MAC: [{2}]";
WMI使用集锦                str
= string.Format(mo["Caption"].ToString(), mo["ServiceName"].ToString(),mo["MACAddress"].ToString());
WMI使用集锦
WMI使用集锦                Response.Write(str); 
WMI使用集锦
WMI使用集锦                
string[] addresses = (string[]) mo[ "IPAddress" ]; 
WMI使用集锦                
string[] subnets = (string[]) mo[ "IPSubnet" ]; 
WMI使用集锦
WMI使用集锦                Response.Write( 
" Addresses :" ); 
WMI使用集锦                
foreach(string sad in addresses) 
WMI使用集锦                    Response.Write(sad
+"<br>"); 
WMI使用集锦
WMI使用集锦                Response.Write( 
" Subnets :" ); 
WMI使用集锦                
foreach(string sub in subnets ) 
WMI使用集锦                    Response.Write(sub
+"<br>"); 
WMI使用集锦            }
 
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
7.如何利用WMI远程重启远程计算机#region 7.如何利用WMI远程重启远程计算机
WMI使用集锦        
private void Button7_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            Response.Write(
"Computer details retrieved using Windows Management Instrumentation (WMI)"); 
WMI使用集锦            Response.Write(
"mailto:[email protected]"); 
WMI使用集锦            Response.Write(
"=========================================================================");  
WMI使用集锦            
//连接远程计算机 
WMI使用集锦
            ConnectionOptions co = new ConnectionOptions(); 
WMI使用集锦            co.Username 
= "john"
WMI使用集锦            co.Password 
= "john"
WMI使用集锦            System.Management.ManagementScope ms 
= new System.Management.ManagementScope("\\\\192.168.1.2\\root\\cimv2", co);       
WMI使用集锦            
//查询远程计算机 
WMI使用集锦
            System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem"); 
WMI使用集锦                   
WMI使用集锦            ManagementObjectSearcher query1 
= new ManagementObjectSearcher(ms,oq); 
WMI使用集锦            ManagementObjectCollection queryCollection1 
= query1.Get();             
WMI使用集锦            
foreach( ManagementObject mo in queryCollection1 )  
WMI使用集锦WMI使用集锦            
WMI使用集锦
WMI使用集锦WMI使用集锦                
string[] ss=WMI使用集锦{""}
WMI使用集锦                mo.InvokeMethod(
"Reboot",ss); 
WMI使用集锦                Response.Write(mo.ToString()); 
WMI使用集锦            }
 
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
8.利用WMI创建一个新的进程#region 8.利用WMI创建一个新的进程
WMI使用集锦        
private void Button8_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            
//Get the object on which the method will be invoked 
WMI使用集锦
            ManagementClass processClass = new ManagementClass("Win32_Process"); 
WMI使用集锦
WMI使用集锦            
//Get an input parameters object for this method 
WMI使用集锦
            ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); 
WMI使用集锦
WMI使用集锦            
//Fill in input parameter values 
WMI使用集锦
            inParams["CommandLine"= "calc.exe"
WMI使用集锦
WMI使用集锦            
//Execute the method 
WMI使用集锦
            ManagementBaseObject outParams = processClass.InvokeMethod ("Create", inParams, null); 
WMI使用集锦
WMI使用集锦            
//Display results 
WMI使用集锦            
//Note: The return code of the method is provided in the "returnvalue" property of the outParams object 
WMI使用集锦
            Response.Write("Creation of calculator process returned: " + outParams["returnvalue"]); 
WMI使用集锦            Response.Write(
"Process ID: " + outParams["processId"]); 
WMI使用集锦
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
9.如何通过WMI终止一个进程#region 9.如何通过WMI终止一个进程
WMI使用集锦        
private void Button9_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            ManagementObject service 
=  
WMI使用集锦                
new ManagementObject("win32_service=\"winmgmt\""); 
WMI使用集锦            InvokeMethodOptions options 
= new InvokeMethodOptions(); 
WMI使用集锦            options.Timeout 
= new TimeSpan(0,0,0,5);  
WMI使用集锦
WMI使用集锦            ManagementBaseObject outParams 
= service.InvokeMethod("StopService"null, options);
WMI使用集锦
WMI使用集锦            Response.Write(
"Return Status = " + outParams["Returnvalue"]);
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
10.如何用WMI 来获取远程机器的目录以及文件#region 10.如何用WMI 来获取远程机器的目录以及文件
WMI使用集锦        
private void Button10_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            ManagementObject disk 
= new ManagementObject(
WMI使用集锦
WMI使用集锦                
"win32_logicaldisk.deviceid=\"c:\"");
WMI使用集锦
WMI使用集锦            disk.Get();
WMI使用集锦
WMI使用集锦            Response.Write(
"Logical Disk Size = " + disk["Size"+ " bytes");
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
11.网卡的MAC地址#region 11.网卡的MAC地址
WMI使用集锦        
private void Button11_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            ManagementClass mc 
= new ManagementClass("Win32_NetworkAdapterConfiguration"); 
WMI使用集锦            ManagementObjectCollection moc 
= mc.GetInstances(); 
WMI使用集锦            
foreach(ManagementObject mo in moc) 
WMI使用集锦WMI使用集锦            
WMI使用集锦
WMI使用集锦                
if((bool)mo["IPEnabled"== true
WMI使用集锦                    Response.Write(
"MAC address"+mo["MacAddress"].ToString()+"<br>"); 
WMI使用集锦                mo.Dispose(); 
WMI使用集锦            }
 
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
12.CPU的系列号#region 12.CPU的系列号 
WMI使用集锦        
private void Button12_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            
string cpuInfo = "";//cpu*** 
WMI使用集锦
            ManagementClass cimobject = new ManagementClass("Win32_Processor"); 
WMI使用集锦            ManagementObjectCollection moc 
= cimobject.GetInstances(); 
WMI使用集锦            
foreach(ManagementObject mo in moc) 
WMI使用集锦WMI使用集锦            
WMI使用集锦
WMI使用集锦                cpuInfo 
= mo.Properties["ProcessorId"].Value.ToString(); 
WMI使用集锦                Response.Write(cpuInfo);
WMI使用集锦            }
 
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
13.主板的系列号#region 13.主板的系列号
WMI使用集锦        
private void Button13_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            ManagementObjectSearcher searcher
=new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
WMI使用集锦            
foreach(ManagementObject share in searcher.Get())
WMI使用集锦WMI使用集锦            
WMI使用集锦{
WMI使用集锦                Response.Write(
"主板制造商:" + share["Manufacturer"].ToString()) ;
WMI使用集锦                Response.Write(
"型号:" +share["Product"].ToString()) ;
WMI使用集锦                Response.Write(
"***:"+share["SerialNumber"].ToString()) ;
WMI使用集锦            }

WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
14.获取硬盘ID#region 14.获取硬盘ID
WMI使用集锦        
private void Button14_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            String HDid; 
WMI使用集锦            ManagementClass cimobject 
= new ManagementClass("Win32_DiskDrive"); 
WMI使用集锦            ManagementObjectCollection moc 
= cimobject.GetInstances(); 
WMI使用集锦            
foreach(ManagementObject mo in moc) 
WMI使用集锦WMI使用集锦            
WMI使用集锦
WMI使用集锦                HDid 
= (string)mo.Properties["Model"].Value; 
WMI使用集锦                Response.Write(HDid);  
WMI使用集锦            }
 
WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦
WMI使用集锦WMI使用集锦        
15.获取本机的用户列表#region 15.获取本机的用户列表
WMI使用集锦        
private void Button15_Click(object sender, System.EventArgs e)
WMI使用集锦WMI使用集锦        
WMI使用集锦{
WMI使用集锦            SelectQuery query 
= new SelectQuery("SELECT * FROM Win32_UserAccount");
WMI使用集锦            ManagementObjectSearcher searcher 
= new ManagementObjectSearcher(query);
WMI使用集锦            
foreach(ManagementObject os in searcher.Get())
WMI使用集锦WMI使用集锦            
WMI使用集锦{
WMI使用集锦                Response.Write(os[
"Name"]);
WMI使用集锦            }

WMI使用集锦        }

WMI使用集锦        
#endregion

WMI使用集锦    }
 

转载于:https://www.cnblogs.com/leonardleonard/archive/2007/03/19/1928386.html