C#: get information about computer in domain

谁说胖子不能爱 提交于 2020-01-14 08:06:46

问题


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)


回答1:


WMI Library and here is a VB.net example. It shouldn't be difficult to convert it to C#




回答2:


Checkout System.Management and System.Management.ManagementClass. Both are used for accessing WMI, which is how to get the information in question.

Edit: Updated with sample to access WMI from remote computer:

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.




回答3:


Here is an example of using it in like an about box. MSDN has the rest of the items you can all.

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;
        }


    }
}



回答4:


Look into the WMI library.



来源:https://stackoverflow.com/questions/971321/c-get-information-about-computer-in-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!