用途:在内网中远程获得计算机的配置信息;
网络环境:基于域管理的网络环境;
开发环境:C# VSTO
客户机要求:XP以上操作系统,同时启动Windows Managerment服务,可以通过组策略开启所有客户机的该项服务。
/*
* 由SharpDevelop创建。
* 用户: eric
* 日期: 2012-02-24
* 时间: 11:07
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections.Generic;
using System.Management;
using System.Windows.Forms ;
namespace RomateComputerManagement
{
/// <summary>
/// Description of MyClass.
/// </summary>
public class RomateComputerManagement
{
public System.Management.ManagementScope Connection(System.Management.ManagementScope ms)
{
try
{
//设定生成的WMI所需的所有设置
System.Management.ConnectionOptions Conn = new ConnectionOptions ( ) ;
//设定用于WMI连接操作的用户名
Conn.Username = AdminName ;
//设定用户的口令
Conn.Password = Password ;
//设定用于执行WMI操作的范围
ms = new ManagementScope ( "\\\\" + RomoteIp + "\\root\\cimv2" , Conn ) ;
ms.Connect();
if(ms.IsConnected)
return ms;
else
return null;
}
catch( Exception ee )
{
//MessageBox.Show ( "连接" + RomoteIp + "出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return null;
}
}
public string GetProcessorInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string cpu="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_Processor" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
cpu=cpu + "CPU:" + Return["NAME"].ToString ( ).Trim() +" " ;
}
return cpu;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得CPU信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetBaseBoardInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string BaseBoard="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_BaseBoard" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
BaseBoard=( "主板:" + Return["SERIALNUMBER"].ToString ( ).Trim()+";"+Return["PRODUCT"].ToString ( ).Trim()+";"+Return["MANUFACTURER"].ToString ( ).Trim() ) ;
}
return BaseBoard;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得主板信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetDisplayConfigurationInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string Display="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_DisplayConfiguration" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
Display=Display + "显卡:" + Return["Description"].ToString ( ).Trim()+" " ;
}
return Display;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得显卡信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetMemoryInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string Memory="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_ComputerSystem" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
Memory= ( "内存:" + Return["TotalPhysicalMemory"].ToString ( ).Trim()) ;
}
return Memory;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得内存信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetNetworkAdapterInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string NetWorkAddpter="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_NetworkAdapterConfiguration" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
if ((bool)Return["IPEnabled"] == true)
{
NetWorkAddpter=NetWorkAddpter + "网卡:" + Return["MacAddress"].ToString ( ).Trim() +" " ;
}
}
return NetWorkAddpter;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得网卡信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetDiskDriveInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string DiskDrive="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_DiskDrive" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
DiskDrive=DiskDrive + "硬盘:" + Return["Model"].ToString ( ).Trim() +" " ;
}
return DiskDrive;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得硬盘信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetLoginUserInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string User="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_ComputerSystem" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
User="当前用户:" + Return["UserName"].ToString ( ).Trim() +" " ;
}
return User;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得用户信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetPCTypeInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string PCType="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_ComputerSystem" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
PCType="PC类型:" + Return["SystemType"].ToString ( ).Trim() +" " ;
}
return PCType;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得PC类型出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetDomainName()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string DomainName="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_ComputerSystem" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
DomainName="域名称:" + Return["Domain"].ToString ( ).Trim() +" " ;
}
return DomainName;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得域名称出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetComputerName()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string ComputerName="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_ComputerSystem" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
ComputerName="计算机名称:" + Return["Name"].ToString ( ).Trim() +" " ;
}
return ComputerName;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得PC名称出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetMonitorInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string Monitor="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_DesktopMonitor" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
Monitor="显示器:" + Return["MonitorType"].ToString ( ).Trim() +" " + Return["MonitorManufacturer"].ToString ( ).Trim() + " " + Return["ScreenHeight"].ToString ( ).Trim() +"*" + Return["ScreenWidth"].ToString ( ).Trim() ;
}
return Monitor;
}
catch(Exception ee)
{
//MessageBox.Show ( "获得显示器信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
private string romoteIp;
public string RomoteIp {
get { return romoteIp; }
set { romoteIp = value; }
}
private string adminName;
public string AdminName {
get { return adminName; }
set { adminName = value; }
}
private string password;
public string Password {
get { return password; }
set { password = value; }
}
}
}
* 由SharpDevelop创建。
* 用户: eric
* 日期: 2012-02-24
* 时间: 11:07
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections.Generic;
using System.Management;
using System.Windows.Forms ;
namespace RomateComputerManagement
{
/// <summary>
/// Description of MyClass.
/// </summary>
public class RomateComputerManagement
{
public System.Management.ManagementScope Connection(System.Management.ManagementScope ms)
{
try
{
//设定生成的WMI所需的所有设置
System.Management.ConnectionOptions Conn = new ConnectionOptions ( ) ;
//设定用于WMI连接操作的用户名
Conn.Username = AdminName ;
//设定用户的口令
Conn.Password = Password ;
//设定用于执行WMI操作的范围
ms = new ManagementScope ( "\\\\" + RomoteIp + "\\root\\cimv2" , Conn ) ;
ms.Connect();
if(ms.IsConnected)
return ms;
else
return null;
}
catch( Exception ee )
{
//MessageBox.Show ( "连接" + RomoteIp + "出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return null;
}
}
public string GetProcessorInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string cpu="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_Processor" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
cpu=cpu + "CPU:" + Return["NAME"].ToString ( ).Trim() +" " ;
}
return cpu;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得CPU信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetBaseBoardInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string BaseBoard="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_BaseBoard" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
BaseBoard=( "主板:" + Return["SERIALNUMBER"].ToString ( ).Trim()+";"+Return["PRODUCT"].ToString ( ).Trim()+";"+Return["MANUFACTURER"].ToString ( ).Trim() ) ;
}
return BaseBoard;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得主板信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetDisplayConfigurationInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string Display="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_DisplayConfiguration" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
Display=Display + "显卡:" + Return["Description"].ToString ( ).Trim()+" " ;
}
return Display;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得显卡信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetMemoryInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string Memory="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_ComputerSystem" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
Memory= ( "内存:" + Return["TotalPhysicalMemory"].ToString ( ).Trim()) ;
}
return Memory;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得内存信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetNetworkAdapterInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string NetWorkAddpter="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_NetworkAdapterConfiguration" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
if ((bool)Return["IPEnabled"] == true)
{
NetWorkAddpter=NetWorkAddpter + "网卡:" + Return["MacAddress"].ToString ( ).Trim() +" " ;
}
}
return NetWorkAddpter;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得网卡信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetDiskDriveInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string DiskDrive="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_DiskDrive" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
DiskDrive=DiskDrive + "硬盘:" + Return["Model"].ToString ( ).Trim() +" " ;
}
return DiskDrive;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得硬盘信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetLoginUserInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string User="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_ComputerSystem" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
User="当前用户:" + Return["UserName"].ToString ( ).Trim() +" " ;
}
return User;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得用户信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetPCTypeInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string PCType="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_ComputerSystem" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
PCType="PC类型:" + Return["SystemType"].ToString ( ).Trim() +" " ;
}
return PCType;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得PC类型出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetDomainName()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string DomainName="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_ComputerSystem" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
DomainName="域名称:" + Return["Domain"].ToString ( ).Trim() +" " ;
}
return DomainName;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得域名称出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetComputerName()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string ComputerName="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_ComputerSystem" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
ComputerName="计算机名称:" + Return["Name"].ToString ( ).Trim() +" " ;
}
return ComputerName;
}
catch(Exception ee)
{
// MessageBox.Show ( "获得PC名称出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
public string GetMonitorInfo()
{
System.Management.ManagementScope ms=new System.Management.ManagementScope();
ms=Connection(ms);
if(ms == null)
return "ERR_CONNECTION";
string Monitor="";
try
{
ObjectQuery Query = new ObjectQuery ( "select * from Win32_DesktopMonitor" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
//通过对产生的WMI的实例集合进行检索,
foreach ( ManagementObject Return in ReturnCollection )
{
Monitor="显示器:" + Return["MonitorType"].ToString ( ).Trim() +" " + Return["MonitorManufacturer"].ToString ( ).Trim() + " " + Return["ScreenHeight"].ToString ( ).Trim() +"*" + Return["ScreenWidth"].ToString ( ).Trim() ;
}
return Monitor;
}
catch(Exception ee)
{
//MessageBox.Show ( "获得显示器信息出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
return "ERR_GET";
}
}
private string romoteIp;
public string RomoteIp {
get { return romoteIp; }
set { romoteIp = value; }
}
private string adminName;
public string AdminName {
get { return adminName; }
set { adminName = value; }
}
private string password;
public string Password {
get { return password; }
set { password = value; }
}
}
}
Computers:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.Office.Interop.Excel;
using RomateComputerManagement;
using System.Threading;
using System.Windows.Forms;
namespace Computers
{
public partial class ExtandRibbon
{
private Thread threadOne;
private event EventHandler OnNumberClear;//数据删除完成引发的事件
private void ExtandRibbon_Load(object sender, RibbonUIEventArgs e)
{
OnNumberClear += new EventHandler(ThreadDemo_OnNumberClear);
}
public void ThreadGetComputer()
{
//threadOne = new Thread(new ThreadStart(Run));
int i = 0;
Range rng2;
Range rng1;
Range rng3;
Range rng4;
Range rng5;
Range rng6;
Range rng7;
Range rng8;
Range rng9;
Range rng10;
Range rng11;
System.Management.ManagementScope ms=new System.Management.ManagementScope();
i = Globals.Computers.UsedRange.Cells.Rows.Count;
for (int j = 2; j <= i; j++)
{
rng2 = (Range)Globals.Computers.Cells[j, 1];
rng1 = (Range)Globals.Computers.Cells[j, 2]; //mac
rng3 = (Range)Globals.Computers.Cells[j, 3]; //user
rng4 = (Range)Globals.Computers.Cells[j, 4]; //pctype
rng5 = (Range)Globals.Computers.Cells[j, 5]; //disk
rng6 = (Range)Globals.Computers.Cells[j, 6]; //BASEBORD
rng7 = (Range)Globals.Computers.Cells[j, 7]; //DISPLAYCARD
rng8 = (Range)Globals.Computers.Cells[j, 8]; //DISPLAY Moniter
rng9 = (Range)Globals.Computers.Cells[j, 9]; //CPU
rng10 = (Range)Globals.Computers.Cells[j, 10]; //MOMERY
rng11 = (Range)Globals.Computers.Cells[j, 11]; //NETACDPTER
RomateComputerManagement.RomateComputerManagement rm = new RomateComputerManagement.RomateComputerManagement();
rm.RomoteIp = rng2.Text;
rm.AdminName = "Administrator"; //通过参数获得
rm.Password = "Sina.com"; //通过参数获得
ms = rm.Connection(ms);
if (ms!=null)
{
rng1.Value = rm.GetNetworkAdapterInfo();
rng3.Value = rm.GetLoginUserInfo();
rng4.Value = rm.GetPCTypeInfo();
rng5.Value = rm.GetDiskDriveInfo();
rng6.Value = rm.GetBaseBoardInfo();
rng7.Value = rm.GetDisplayConfigurationInfo();
rng8.Value = rm.GetMonitorInfo();
rng9.Value = rm.GetProcessorInfo();
rng10.Value = rm.GetMemoryInfo();
rng11.Value = rm.GetNetworkAdapterInfo();
}
else
rng1.Value = "ERROR";
}
OnNumberClear(this, new EventArgs());//引发完成事件
}
//执行完成之后,停止所有线程
void ThreadDemo_OnNumberClear(object sender, EventArgs e)
{
MessageBox.Show("导入完毕!");
threadOne.Abort();
}
private void GetComputer_Click(object sender, RibbonControlEventArgs e)
{
threadOne = new Thread(new ThreadStart(ThreadGetComputer));
threadOne.Start();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.Office.Interop.Excel;
using RomateComputerManagement;
using System.Threading;
using System.Windows.Forms;
namespace Computers
{
public partial class ExtandRibbon
{
private Thread threadOne;
private event EventHandler OnNumberClear;//数据删除完成引发的事件
private void ExtandRibbon_Load(object sender, RibbonUIEventArgs e)
{
OnNumberClear += new EventHandler(ThreadDemo_OnNumberClear);
}
public void ThreadGetComputer()
{
//threadOne = new Thread(new ThreadStart(Run));
int i = 0;
Range rng2;
Range rng1;
Range rng3;
Range rng4;
Range rng5;
Range rng6;
Range rng7;
Range rng8;
Range rng9;
Range rng10;
Range rng11;
System.Management.ManagementScope ms=new System.Management.ManagementScope();
i = Globals.Computers.UsedRange.Cells.Rows.Count;
for (int j = 2; j <= i; j++)
{
rng2 = (Range)Globals.Computers.Cells[j, 1];
rng1 = (Range)Globals.Computers.Cells[j, 2]; //mac
rng3 = (Range)Globals.Computers.Cells[j, 3]; //user
rng4 = (Range)Globals.Computers.Cells[j, 4]; //pctype
rng5 = (Range)Globals.Computers.Cells[j, 5]; //disk
rng6 = (Range)Globals.Computers.Cells[j, 6]; //BASEBORD
rng7 = (Range)Globals.Computers.Cells[j, 7]; //DISPLAYCARD
rng8 = (Range)Globals.Computers.Cells[j, 8]; //DISPLAY Moniter
rng9 = (Range)Globals.Computers.Cells[j, 9]; //CPU
rng10 = (Range)Globals.Computers.Cells[j, 10]; //MOMERY
rng11 = (Range)Globals.Computers.Cells[j, 11]; //NETACDPTER
RomateComputerManagement.RomateComputerManagement rm = new RomateComputerManagement.RomateComputerManagement();
rm.RomoteIp = rng2.Text;
rm.AdminName = "Administrator"; //通过参数获得
rm.Password = "Sina.com"; //通过参数获得
ms = rm.Connection(ms);
if (ms!=null)
{
rng1.Value = rm.GetNetworkAdapterInfo();
rng3.Value = rm.GetLoginUserInfo();
rng4.Value = rm.GetPCTypeInfo();
rng5.Value = rm.GetDiskDriveInfo();
rng6.Value = rm.GetBaseBoardInfo();
rng7.Value = rm.GetDisplayConfigurationInfo();
rng8.Value = rm.GetMonitorInfo();
rng9.Value = rm.GetProcessorInfo();
rng10.Value = rm.GetMemoryInfo();
rng11.Value = rm.GetNetworkAdapterInfo();
}
else
rng1.Value = "ERROR";
}
OnNumberClear(this, new EventArgs());//引发完成事件
}
//执行完成之后,停止所有线程
void ThreadDemo_OnNumberClear(object sender, EventArgs e)
{
MessageBox.Show("导入完毕!");
threadOne.Abort();
}
private void GetComputer_Click(object sender, RibbonControlEventArgs e)
{
threadOne = new Thread(new ThreadStart(ThreadGetComputer));
threadOne.Start();
}
}
}