C#:获取域中计算机的信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/971321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: get information about computer in domain
提问by melculetz
What classes should I use in C# in order to get information about a certain computer in my network? (Like who is logged on that computer, what Operating System is running on that computer, what ports are opened etc)
我应该在 C# 中使用哪些类来获取有关网络中某台计算机的信息?(例如谁登录了该计算机,该计算机上运行的是什么操作系统,打开了哪些端口等)
采纳答案by Shoban
WMI Library and here is a VB.net example. It shouldn't be difficult to convert it to C#
WMI 库,这里是一个VB.net 示例。将其转换为 C# 应该不难
回答by Assaf Lavie
Look into the WMI library.
查看 WMI 库。
回答by Nate
Checkout System.Managementand System.Management.ManagementClass. Both are used for accessing WMI, which is how to get the information in question.
签出System.Management和System.Management.ManagementClass。两者都用于访问WMI,即如何获取有问题的信息。
Edit:Updated with sample to access WMI from remote computer:
编辑:更新示例以从远程计算机访问 WMI:
ConnectionOptions options;
options = new ConnectionOptions();
options.Username = userID;
options.Password = password;
options.EnablePrivileges = true;
options.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope scope;
scope = new ManagementScope("\\" + ipAddress + "\root\cimv2", options);
scope.Connect();
String queryString = "SELECT PercentProcessorTime, PercentInterruptTime, InterruptsPersec FROM Win32_PerfFormattedData_PerfOS_Processor";
ObjectQuery query;
query = new ObjectQuery(queryString);
ManagementObjectSearcher objOS = new ManagementObjectSearcher(scope, query);
DataTable dt = new DataTable();
dt.Columns.Add("PercentProcessorTime");
dt.Columns.Add("PercentInterruptTime");
dt.Columns.Add("InterruptsPersec");
foreach (ManagementObject MO in objOS.Get())
{
DataRow dr = dt.NewRow();
dr["PercentProcessorTime"] = MO["PercentProcessorTime"];
dr["PercentInterruptTime"] = MO["PercentInterruptTime"];
dr["InterruptsPersec"] = MO["InterruptsPersec"];
dt.Rows.Add(dr);
}
Note: userID, password, and ipAddress must all be defined to match your environment.
注意:用户 ID、密码和 ipAddress 都必须定义为与您的环境相匹配。
回答by mcauthorn
Here is an example of using it in like an about box. MSDN has the rest of the items you can all.
这是一个使用它的例子,就像一个关于框。MSDN 拥有您可以全部使用的其余项目。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace About_box
{
public partial class About : Form
{
public About()
{
InitializeComponent();
FormLoad();
}
public void FormLoad()
{
SystemInfo si;
SystemInfo.GetSystemInfo(out si);
txtboxApplication.Text = si.AppName;
txtboxVersion.Text = si.AppVersion;
txtBoxComputerName.Text = si.MachineName;
txtBoxMemory.Text = Convert.ToString((si.TotalRam / 1073741824)
+ " GigaBytes");
txtBoxProcessor.Text = si.ProcessorName;
txtBoxOperatingSystem.Text = si.OperatingSystem;
txtBoxOSVersion.Text = si.OperatingSystemVersion;
txtBoxManufacturer.Text = si.Manufacturer;
txtBoxModel.Text = si.Model;
}
}
}