C#:获取有关域中计算机的信息

时间:2009-06-09 17:05:05

标签: c# .net network-programming

我应该在C#中使用哪些类来获取有关我网络中某台计算机的信息? (比如谁登录该计算机,该计算机上运行的操作系统,打开的端口等)

4 个答案:

答案 0 :(得分:9)

结帐System.ManagementSystem.Management.ManagementClass。两者都用于访问WMI,这是如何获取有问题的信息。

编辑:更新了示例以从远程计算机访问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);
}

注意:必须定义userID,password和ipAddress以匹配您的环境。

答案 1 :(得分:3)

这是一个像关于盒子一样使用它的例子。 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;
        }


    }
}

答案 2 :(得分:2)

查看WMI库。

答案 3 :(得分:2)

WMI库,这里是VB.net example。将它转换为C#

应该不难
相关问题