【问题标题】:C#: get information about computer in domainC#:获取域中计算机的信息
【发布时间】:2009-06-09 17:05:05
【问题描述】:

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

【问题讨论】:

  • 您是否尝试在目标机器上运行此代码?还是您尝试从您的计算机上运行它并通过网络获取有关另一台计算机的信息?
  • 我想从我的计算机运行代码以通过网络获取另一台机器的信息,知道它的 IP
  • 在我的回答中提供了远程 WMI 查询的示例。

标签: c# .net network-programming


【解决方案1】:

结帐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);
}

注意:用户 ID、密码和 ipAddress 都必须定义为与您的环境相匹配。

【讨论】:

    【解决方案2】:

    这是一个在 about 框中使用它的示例。 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;
            }
    
    
        }
    }
    

    【讨论】:

    • 这仅在代码在相关机器上运行时才有效;并且无法获取网络上另一台机器的信息。
    • 这个类(SystemInfo)在哪里定义的?
    【解决方案3】:

    WMI 库,这里是 VB.net example。转换成C#应该不难

    【讨论】:

      【解决方案4】:

      查看 WMI 库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-04
        • 2013-01-04
        • 1970-01-01
        • 1970-01-01
        • 2017-01-01
        相关资源
        最近更新 更多