1.WMI简介
WMI是英文WindowsManagementInstrumentation的简写,它的功能主要是:访问本地主机的一些信息和服务,可以管理远程计算机(当然你必须要拥有足够的权限),比如:重启,关机,关闭进程,创建进程等。
2.使用时首先添加System.Management.dll,然后引用
usingSystem.Management;
usingSystem.Threading;
3.示例代码
3.1 html代码
<HTML>
<HEAD>
<title>WMITest</title>
<metaname="GENERATOR"Content="MicrosoftVisualStudio.NET7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="Form1"method="post"runat="server">
<FONTface="宋体">
<asp:Buttonid="Button1"style="Z-INDEX:101;LEFT:48px;POSITION:absolute;TOP:16px"runat="server"
Text="获得本地磁盘的信息"Width="152px"></asp:Button>
<asp:Buttonid="Button2"style="Z-INDEX:102;LEFT:32px;POSITION:absolute;TOP:56px"runat="server"
Text="获得指定磁盘的容量"Width="144px"></asp:Button>
<asp:Buttonid="Button3"style="Z-INDEX:103;LEFT:32px;POSITION:absolute;TOP:96px"runat="server"
Text="列出机器中所有的共享资源"Width="192px"></asp:Button>
<asp:Buttonid="Button4"style="Z-INDEX:104;LEFT:8px;POSITION:absolute;TOP:128px"runat="server"
Text="控制让系统中的某个文件夹共享或取消共享"Width="264px"></asp:Button>
<asp:Buttonid="Button5"style="Z-INDEX:105;LEFT:48px;POSITION:absolute;TOP:160px"runat="server"
Text="获得系统服务的运行状态"></asp:Button>
<asp:Buttonid="Button6"style="Z-INDEX:106;LEFT:40px;POSITION:absolute;TOP:192px"runat="server"
Text="通过WMI修改IP,而实现不用重新启动"Width="248px"></asp:Button>
<asp:Buttonid="Button7"style="Z-INDEX:107;LEFT:24px;POSITION:absolute;TOP:224px"runat="server"
Text="如何利用WMI远程重启远程计算机"Width="232px"></asp:Button>
<asp:Buttonid="Button8"style="Z-INDEX:108;LEFT:40px;POSITION:absolute;TOP:264px"runat="server"
Text="利用WMI创建一个新的进程"Width="192px"></asp:Button>
<asp:Buttonid="Button9"style="Z-INDEX:109;LEFT:32px;POSITION:absolute;TOP:296px"runat="server"
Text="如何通过WMI终止一个进程"Width="192px"></asp:Button>
<asp:Buttonid="Button10"style="Z-INDEX:110;LEFT:32px;POSITION:absolute;TOP:328px"runat="server"
Text="如何用WMI来获取远程机器的目录以及文件"Width="280px"></asp:Button>
<asp:Buttonid="Button11"style="Z-INDEX:111;LEFT:424px;POSITION:absolute;TOP:24px"runat="server"
Text="网卡的MAC地址"Width="120px"></asp:Button>
<asp:Buttonid="Button12"style="Z-INDEX:112;LEFT:456px;POSITION:absolute;TOP:64px"runat="server"
Text="CPU的系列号"Width="104px"></asp:Button>
<asp:Buttonid="Button13"style="Z-INDEX:113;LEFT:448px;POSITION:absolute;TOP:120px"
runat="server"Text="主板的系列号"></asp:Button>
<asp:Buttonid="Button14"style="Z-INDEX:114;LEFT:472px;POSITION:absolute;TOP:160px"
runat="server"Text="获取硬盘ID"></asp:Button>
<asp:Buttonid="Button15"style="Z-INDEX:115;LEFT:448px;POSITION:absolute;TOP:208px"
runat="server"Text="获取本机的用户列表"Width="144px"></asp:Button></FONT>
</form>
</body>
</HTML>
3.2cs代码
publicclassWMITest:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.ButtonButton2;
protectedSystem.Web.UI.WebControls.ButtonButton3;
protectedSystem.Web.UI.WebControls.ButtonButton4;
protectedSystem.Web.UI.WebControls.ButtonButton5;
protectedSystem.Web.UI.WebControls.ButtonButton6;
protectedSystem.Web.UI.WebControls.ButtonButton7;
protectedSystem.Web.UI.WebControls.ButtonButton8;
protectedSystem.Web.UI.WebControls.ButtonButton9;
protectedSystem.Web.UI.WebControls.ButtonButton10;
protectedSystem.Web.UI.WebControls.ButtonButton11;
protectedSystem.Web.UI.WebControls.ButtonButton12;
protectedSystem.Web.UI.WebControls.ButtonButton13;
protectedSystem.Web.UI.WebControls.ButtonButton14;
protectedSystem.Web.UI.WebControls.ButtonButton15;
protectedSystem.Web.UI.WebControls.ButtonButton1;
privatevoidPage_Load(objectsender,System.EventArgse)
{
//Putusercodetoinitializethepagehere
}
WebFormDesignergeneratedcode#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.
//
InitializeComponent();
base.OnInit(e);
}
/**////<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
privatevoidInitializeComponent()
{
this.Button1.Click+=newSystem.EventHandler(this.Button1_Click);
this.Button2.Click+=newSystem.EventHandler(this.Button2_Click);
this.Button3.Click+=newSystem.EventHandler(this.Button3_Click);
this.Button4.Click+=newSystem.EventHandler(this.Button4_Click);
this.Button5.Click+=newSystem.EventHandler(this.Button5_Click);
this.Button6.Click+=newSystem.EventHandler(this.Button6_Click);
this.Button7.Click+=newSystem.EventHandler(this.Button7_Click);
this.Button8.Click+=newSystem.EventHandler(this.Button8_Click);
this.Button9.Click+=newSystem.EventHandler(this.Button9_Click);
this.Button10.Click+=newSystem.EventHandler(this.Button10_Click);
this.Button11.Click+=newSystem.EventHandler(this.Button11_Click);
this.Button12.Click+=newSystem.EventHandler(this.Button12_Click);
this.Button13.Click+=newSystem.EventHandler(this.Button13_Click);
this.Button14.Click+=newSystem.EventHandler(this.Button14_Click);
this.Button15.Click+=newSystem.EventHandler(this.Button15_Click);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
1.如何用WMI获得指定磁盘的容量#region1.如何用WMI获得指定磁盘的容量
privatestringDriveType(stringtype)
{
stringrtntype="";
switch(type)
{
case"1":
rtntype="NotType";
break;
case"2":
rtntype="Floppydisk";
break;
case"3":
rtntype="Harddisk";
break;
case"4":
rtntype="Removabledriveornetworkdrive";
break;
case"5":
rtntype="CD-ROM";
break;
case"6":
rtntype="RAMdisk";
break;
default:
break;
}
returnrtntype;
}
privatevoidButton1_Click(objectsender,System.EventArgse)
{
SelectQueryquery=newSelectQuery("Select*FromWin32_LogicalDisk");
ManagementObjectSearchersearcher=newManagementObjectSearcher(query);
foreach(ManagementBaseObjectdiskinsearcher.Get())
{
Response.Write(disk["Name"]+""+DriveType(disk["DriveType"].ToString())+""+disk["VolumeName"]+"<br>");
}
}
#endregion
2.如何用WMI获得指定磁盘的容量#region2.如何用WMI获得指定磁盘的容量
privatevoidButton2_Click(objectsender,System.EventArgse)
{
ManagementObjectdisk=newManagementObject("win32_logicaldisk.deviceid=/"c:/"");
disk.Get();
Response.Write("LogicalDiskSize="+disk["Size"]+"bytes");
}
#endregion
3.如何列出机器中所有的共享资源#region3.如何列出机器中所有的共享资源
privatevoidButton3_Click(objectsender,System.EventArgse)
{
ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_share");
foreach(ManagementObjectshareinsearcher.Get())
{
Response.Write(share.GetText(TextFormat.Mof));
}
}
#endregion
4.怎样写程控制让系统中的某个文件夹共享或取消共享#region4.怎样写程控制让系统中的某个文件夹共享或取消共享
privatevoidButton4_Click(objectsender,System.EventArgse)
{
/**//*首先,这需要以有相应权限的用户登录系统才行
将
object[]obj={"C://Temp","我的共享",0,10,"DotNet实现的共享",""};
改为
object[]obj={"C://Temp","我的共享",0,null,"DotNet实现的共享",""};
就可以实现授权给最多用户了。
*/
ManagementClass_class=newManagementClass(newManagementPath("Win32_Share"));
object[]obj={"C://Temp","我的共享",0,10,"DotNet实现的共享",""};
_class.InvokeMethod("create",obj);
}
#endregion
5.如何获得系统服务的运行状态#region5.如何获得系统服务的运行状态
privatevoidButton5_Click(objectsender,System.EventArgse)
{
string[]lvData=newstring[4];
ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_Service");
foreach(ManagementObjectmoinsearcher.Get())
{
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();
Response.Write(lvData[0]+lvData[1]+lvData[2]+lvData[3]);
}
}
#endregion
6.通过WMI修改IP,而实现不用重新启动#region6.通过WMI修改IP,而实现不用重新启动
privatevoidButton6_Click(objectsender,System.EventArgse)
{
ReportIP();
//SwitchToDHCP();
SwitchToprivate();
Thread.Sleep(5000);
ReportIP();
Response.Write("end.");
}
privatevoidSwitchToDHCP()
{
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;
}
}
privatevoidSwitchToprivate()
{
ManagementBaseObjectinPar=null;
ManagementBaseObjectoutPar=null;
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
{
if(!(bool)mo["IPEnabled"])
continue;
inPar=mo.GetMethodParameters("Enableprivate");
inPar["IPAddress"]=newstring[]{"192.168.1.1"};
inPar["SubnetMask"]=newstring[]{"255.255.255.0"};
outPar=mo.InvokeMethod("Enableprivate",inPar,null);
break;
}
}
privatevoidReportIP()
{
Response.Write("******CurrentIPaddresses:");
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
{
if(!(bool)mo["IPEnabled"])
continue;
stringstr="{0}/nSVC:'{1}'MAC:[{2}]";
str=string.Format(mo["Caption"].ToString(),mo["ServiceName"].ToString(),mo["MACAddress"].ToString());
Response.Write(str);
string[]addresses=(string[])mo["IPAddress"];
string[]subnets=(string[])mo["IPSubnet"];
Response.Write("Addresses:");
foreach(stringsadinaddresses)
Response.Write(sad+"<br>");
Response.Write("Subnets:");
foreach(stringsubinsubnets)
Response.Write(sub+"<br>");
}
}
#endregion
7.如何利用WMI远程重启远程计算机#region7.如何利用WMI远程重启远程计算机
privatevoidButton7_Click(objectsender,System.EventArgse)
{
Response.Write("ComputerdetailsretrievedusingWindowsManagementInstrumentation(WMI)");
Response.Write("mailto:singlepine@hotmail.com");
Response.Write("=========================================================================");
//连接远程计算机
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=newManagementObjectSearcher(ms,oq);
ManagementObjectCollectionqueryCollection1=query1.Get();
foreach(ManagementObjectmoinqueryCollection1)
{
string[]ss={""};
mo.InvokeMethod("Reboot",ss);
Response.Write(mo.ToString());
}
}
#endregion
8.利用WMI创建一个新的进程#region8.利用WMI创建一个新的进程
privatevoidButton8_Click(objectsender,System.EventArgse)
{
//Gettheobjectonwhichthemethodwillbeinvoked
ManagementClassprocessClass=newManagementClass("Win32_Process");
//Getaninputparametersobjectforthismethod
ManagementBaseObjectinParams=processClass.GetMethodParameters("Create");
//Fillininputparametervalues
inParams["CommandLine"]="calc.exe";
//Executethemethod
ManagementBaseObjectoutParams=processClass.InvokeMethod("Create",inParams,null);
//Displayresults
//Note:Thereturncodeofthemethodisprovidedinthe"returnvalue"propertyoftheoutParamsobject
Response.Write("Creationofcalculatorprocessreturned:"+outParams["returnvalue"]);
Response.Write("ProcessID:"+outParams["processId"]);
}
#endregion
9.如何通过WMI终止一个进程#region9.如何通过WMI终止一个进程
privatevoidButton9_Click(objectsender,System.EventArgse)
{
ManagementObjectservice=
newManagementObject("win32_service=/"winmgmt/"");
InvokeMethodOptionsoptions=newInvokeMethodOptions();
options.Timeout=newTimeSpan(0,0,0,5);
ManagementBaseObjectoutParams=service.InvokeMethod("StopService",null,options);
Response.Write("ReturnStatus="+outParams["Returnvalue"]);
}
#endregion
10.如何用WMI来获取远程机器的目录以及文件#region10.如何用WMI来获取远程机器的目录以及文件
privatevoidButton10_Click(objectsender,System.EventArgse)
{
ManagementObjectdisk=newManagementObject(
"win32_logicaldisk.deviceid=/"c:/"");
disk.Get();
Response.Write("LogicalDiskSize="+disk["Size"]+"bytes");
}
#endregion
11.网卡的MAC地址#region11.网卡的MAC地址
privatevoidButton11_Click(objectsender,System.EventArgse)
{
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
{
if((bool)mo["IPEnabled"]==true)
Response.Write("MACaddress"+mo["MacAddress"].ToString()+"<br>");
mo.Dispose();
}
}
#endregion
12.CPU的系列号#region12.CPU的系列号
privatevoidButton12_Click(objectsender,System.EventArgse)
{
stringcpuInfo="";//cpu序列号
ManagementClasscimobject=newManagementClass("Win32_Processor");
ManagementObjectCollectionmoc=cimobject.GetInstances();
foreach(ManagementObjectmoinmoc)
{
cpuInfo=mo.Properties["ProcessorId"].Value.ToString();
Response.Write(cpuInfo);
}
}
#endregion
13.主板的系列号#region13.主板的系列号
privatevoidButton13_Click(objectsender,System.EventArgse)
{
ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_BaseBoard");
foreach(ManagementObjectshareinsearcher.Get())
{
Response.Write("主板制造商:"+share["Manufacturer"].ToString());
Response.Write("型号:"+share["Product"].ToString());
Response.Write("序列号:"+share["SerialNumber"].ToString());
}
}
#endregion
14.获取硬盘ID#region14.获取硬盘ID
privatevoidButton14_Click(objectsender,System.EventArgse)
{
StringHDid;
ManagementClasscimobject=newManagementClass("Win32_DiskDrive");
ManagementObjectCollectionmoc=cimobject.GetInstances();
foreach(ManagementObjectmoinmoc)
{
HDid=(string)mo.Properties["Model"].Value;
Response.Write(HDid);
}
}
#endregion
15.获取本机的用户列表#region15.获取本机的用户列表
privatevoidButton15_Click(objectsender,System.EventArgse)
{
SelectQueryquery=newSelectQuery("SELECT*FROMWin32_UserAccount");
ManagementObjectSearchersearcher=newManagementObjectSearcher(query);
foreach(ManagementObjectosinsearcher.Get())
{
Response.Write(os["Name"]);
}
}
#endregion
}
分享到:
相关推荐
wmi 的使用方法实例,自己看吧。不多说,方便有相同需求的同学
里面集合了一些微软官方的wmi代码,可作为参考.
WMI使用的WIN32_类库名 包括:硬件类、操作系统类、安装应用程序类、WMI服务管理类、性能计数器类
WMI(Windows Management Instrumentation,Windows 管理规范)是一项核心的 Windows 管理技术;用户可以使用 WMI 管理本地和远程计算机。 WMI查询可通过WMI命令查询系统的信息,本文档演示如何使用WMI查询。
使用wmi获取设备信息,wmi可用类众多,详细使用可以搜索msdn。这里用获取硬盘序列号等信息作为示例。在vs2008编译运行。
Visual C++源代码 87 如何使用WMI查询处理器信息Visual C++源代码 87 如何使用WMI查询处理器信息Visual C++源代码 87 如何使用WMI查询处理器信息Visual C++源代码 87 如何使用WMI查询处理器信息Visual C++源代码 87 ...
Visual C++源代码 84 如何使用WMI查询声音设备信息Visual C++源代码 84 如何使用WMI查询声音设备信息Visual C++源代码 84 如何使用WMI查询声音设备信息Visual C++源代码 84 如何使用WMI查询声音设备信息Visual C++源...
Visual C++源代码 82 如何使用WMI条件查询系统服务Visual C++源代码 82 如何使用WMI条件查询系统服务Visual C++源代码 82 如何使用WMI条件查询系统服务Visual C++源代码 82 如何使用WMI条件查询系统服务Visual C++源...
Visual C++源代码 86 如何使用WMI查询串行端口信息Visual C++源代码 86 如何使用WMI查询串行端口信息Visual C++源代码 86 如何使用WMI查询串行端口信息Visual C++源代码 86 如何使用WMI查询串行端口信息Visual C++源...
使用wmi条件查询系统服务
修复WMI服务 修复WMI服务 修复WMI服务
WINdows wmi编程 C#实例 里面很清楚的介绍了好几个常见的WMI的查询
使用wmi查询显示设备信息
使用wmi查询驱动设备信息
使用wmi查询处理器信息
使用wmi查询串行端口信息
系统管理编程实例:如何使用WMI查询处理器信息
WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI WMI v
C#非wmi获取CPU处理器使用率,各核心使用率。
关于WMI,WIN_NETWORKADAPTERCONFIGURATION网络管理