2009-06-09 14 views
7

ネットワーク上の特定のコンピュータに関する情報を取得するために、C#でどのクラスを使用する必要がありますか? (誰がそのコンピュータにログオンしているのか、そのコンピュータで実行されているオペレーティングシステム、開いているポートなど)C#:ドメイン内のコンピュータに関する情報を取得

+1

このコードをターゲットマシンで実行しようとしていますか?またはあなたのコンピュータからこれを実行し、ネットワーク上の別のマシンに関する情報を取得しようとしていますか? – Nate

+0

自分のコンピュータからコードを実行して、ネットワーク上の別のマシンに関する情報を取得し、そのIPアドレスを知りたい – melculetz

+0

私の答えにリモートWMIクエリーの例が示されています。 – Nate

答えて

2

WMIライブラリです。ここにはVB.net exampleがあります。 C#に変換するのは難しいことではありません。

2

WMIライブラリを調べます。

9

チェックアウトSystem.ManagementSystem.Management.ManagementClassです。両方ともWMIへのアクセスに使用されます.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のは、すべての環境に合わせて定義する必要があります。

+0

リンクありがとうございました – melculetz

3

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


    } 
} 
+1

これは、問題のマシンでコードが実行されている場合にのみ機能します。ネットワーク上の別のマシンに関する情報を取得することはできません。 – Nate

+4

このクラス(SystemInfo)はどこに定義されていますか? – SLaks

関連する問題