2009-06-09 56 views
7

我應該在C#中使用哪些類以便獲取有關網絡中某臺計算機的信息? (例如誰在該計算機上登錄,在該計算機上運行什麼操作系統,打開哪些端口等)C#:獲取有關域中計算機的信息

+1

您是否試圖在目標機器上運行此代碼?或者您是否試圖通過計算機運行此程序,並通過網絡獲取有關另一臺計算機的信息? – Nate 2009-06-09 17:26:16

+0

我想從我的計算機上運行代碼,以便通過網絡獲取有關另一臺計算機的信息,瞭解其IP。 – melculetz 2009-06-09 18:51:49

+0

在我的答案中提供了遠程WMI查詢的示例。 – Nate 2009-06-09 20:52:00

回答

2

查看WMI庫。

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

注:用戶ID,密碼和ip地址都必須被定義爲匹配您的環境。

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


    } 
}