2012-02-09 48 views
1

大家好,如何獲取計算機說明?

我如何編程獲取計算機描述? 我正在使用C#和.NET 2.0。

enter image description here

我試圖Console.WriteLine(Dns.GetHostName());,但它呼應了Full computer name代替。

我還使用了下面的代碼:

ManagementObjectSearcher query1 = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem") ; 
ManagementObjectCollection queryCollection1 = query1.Get(); 

foreach(ManagementObject mo in queryCollection1) 
{ 
    Console.WriteLine(mo["Description"].ToString()); 
} 

但這似乎不工作,我得到這個異常:

Exception System.IO.FileNotFoundException was thrown in debuggee: Could not load file or assembly 'System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

+0

您是否安裝了PowerShell? – 2012-02-09 13:31:32

+0

你的第二種方法是正確的,但你得到的例外很奇怪。你如何添加對System.Management的引用? – 2012-02-09 13:32:00

+0

@ AvnerShahar-Kashtan:通過使用System.Management進口它''我也將它添加到我的項目的參考中。 – yonan2236 2012-02-09 13:33:10

回答

7

這是在註冊表va略

HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\srvcomment

最簡單的方法來訪問它會是:

using Microsoft.Win32; 

string key = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters"; 
string computerDescription = (string)Registry.GetValue(key, "srvcomment", null); 
+0

是的!這是工作!謝謝ssg:) – yonan2236 2012-02-09 13:42:06

+1

請,請,請不要這樣做。此註冊表項是[技術上記錄](http://technet.microsoft.com/en-us/library/cc787346(v = ws.10).aspx),但這不是查詢此信息的受支持方式。將'NetServerGetInfo'與'SERVER_INFO_101'一起使用 - 這將通過C/C++,Pinvoke或JNI提供。特別是一些AV產品鎖定了註冊碼,但Net API仍在繼續工作。 – 2013-08-21 15:05:52

+0

@NicholasWilson NetServerGetInfo似乎非常參與查詢,結構和所有PInvoke定義。 – 2013-08-21 15:18:11

0

下面的代碼將獲得計算機描述。我沒有在.NET 2.0上測試這個,但是從v1.1開始使用的管理類已經出現了,所以它應該可以工作。

 using System.Management; 

     string description; 

     using (ManagementClass mc = new ManagementClass("Win32_OperatingSystem")) 
     using (ManagementObjectCollection moc = mc.GetInstances()) 
     { 
      foreach (ManagementObject mo in moc) 
      { 
       if (mo.Properties["Description"] != null) 
       { 
        description = mo.Properties["Description"].Value.ToString(); 
        break; 
       } 
      } 
     }