关于获取服务器性能参数的难题? 假设我现在知道了服务器A的IP,在我的客户机B上运行的程序想要获得A的一些性能参数,比如CPU使用率,内存使用量,网络速度等,应该怎么办?我是在.NET中使用C#中写程序的,需要引用哪些命名空间?使用哪些类和函数?谢谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 System.Management命名空间里面提供了相关方法,注意ConnectionOptions类\ManagementScope 类登几个类,示例:long mb = 1048576 ; //1024x1024 //设定生成的WMI所需的所有设置 System.Management.ConnectionOptions Conn = new ConnectionOptions ( ) ; //设定用于WMI连接操作的用户名 Conn.Username = 用户名; //设定用户的口令 Conn.Password = 密码; //设定用于执行WMI操作的范围 System.Management.ManagementScope Ms = new ManagementScope ( "\\\\" + 机器名或ip + "\\root\\cimv2" , Conn ) ; try { //连接到实际操作的WMI范围 Ms.Connect ( ) ; //设定通过WMI要查询的内容 ObjectQuery Query = new ObjectQuery ( "select FreeSpace ,Size ,Name from Win32_LogicalDisk where DriveType=3" ) ; //WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合 ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( Ms , Query ) ; //异步调用WMI查询 ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ; double free = 0 ; double use = 0 ; double total = 0 ; listBox1.Items.Clear ( ) ; //通过对产生的WMI的实例集合进行检索,获得硬盘信息 foreach ( ManagementObject Return in ReturnCollection ) { listBox1.Items.Add ( "磁盘名称:" + Return["Name"].ToString ( ) ) ; //获得硬盘的可用空间 free = Convert.ToInt64 ( Return["FreeSpace"] ) /mb ; //获得硬盘的已用空间 use = ( Convert.ToInt64 ( Return["Size"] ) - Convert.ToInt64 ( Return["FreeSpace"] ) ) /mb ; //获得硬盘的合计空间 total = Convert.ToInt64 ( Return["Size"] ) /mb ; listBox1.Items.Add ( " 总计:"+ total.ToString ( ) + "MB" ) ; listBox1.Items.Add ( "已用空间:"+ use.ToString ( ) + "MB" ) ; listBox1.Items.Add ( "可用空间:"+ free.ToString ( ) + "MB" ) ; } } catch ( Exception ee ) { MessageBox.Show ( "连接" + textBox1.Text + "出错,出错信息为:" + ee.Message ,"出现错误!" ) ; } 对于网络不文档的winForm程序控制 远程协同开发问题 汉字串编码的问题 如何存取图像(在SQL Server数据库中) 怎么在程序中得到数据库字段长度? 关于GPS定位系统中电子地图开发问题 请问如何遍历用户控件里的Checkbox? datagrid的简单问题马上给分 打不开解决方案 如何让一个textBox中只能输入一个小数点? mediaplayer 控件和listview如何关联? 问一下序列化和反序列化的问题,实在是没办法了
//1024x1024
//设定生成的WMI所需的所有设置
System.Management.ConnectionOptions Conn = new ConnectionOptions ( ) ;
//设定用于WMI连接操作的用户名
Conn.Username = 用户名;
//设定用户的口令
Conn.Password = 密码;
//设定用于执行WMI操作的范围
System.Management.ManagementScope Ms = new ManagementScope ( "\\\\" + 机器名或ip + "\\root\\cimv2" , Conn ) ;
try
{
//连接到实际操作的WMI范围
Ms.Connect ( ) ;
//设定通过WMI要查询的内容
ObjectQuery Query = new ObjectQuery ( "select FreeSpace ,Size ,Name from Win32_LogicalDisk where DriveType=3" ) ;
//WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合
ManagementObjectSearcher Searcher = new ManagementObjectSearcher ( Ms , Query ) ;
//异步调用WMI查询
ManagementObjectCollection ReturnCollection = Searcher.Get ( ) ;
double free = 0 ;
double use = 0 ;
double total = 0 ;
listBox1.Items.Clear ( ) ;
//通过对产生的WMI的实例集合进行检索,获得硬盘信息
foreach ( ManagementObject Return in ReturnCollection )
{
listBox1.Items.Add ( "磁盘名称:" + Return["Name"].ToString ( ) ) ;
//获得硬盘的可用空间
free = Convert.ToInt64 ( Return["FreeSpace"] ) /mb ;
//获得硬盘的已用空间
use = ( Convert.ToInt64 ( Return["Size"] ) - Convert.ToInt64 ( Return["FreeSpace"] ) ) /mb ;
//获得硬盘的合计空间
total = Convert.ToInt64 ( Return["Size"] ) /mb ;
listBox1.Items.Add ( " 总计:"+ total.ToString ( ) + "MB" ) ;
listBox1.Items.Add ( "已用空间:"+ use.ToString ( ) + "MB" ) ;
listBox1.Items.Add ( "可用空间:"+ free.ToString ( ) + "MB" ) ;
}
}
catch ( Exception ee )
{
MessageBox.Show ( "连接" + textBox1.Text + "出错,出错信息为:" + ee.Message ,"出现错误!" ) ;
}