如何获取计算机描述?

时间:2012-02-09 13:28:12

标签: c# .net-2.0

大家好,

我如何以编程方式获取计算机描述? 我正在使用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.

3 个答案:

答案 0 :(得分:8)

它在注册表值

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

答案 1 :(得分:3)

以下代码将获取计算机描述。我没有在.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;
                }
            }
        }

答案 2 :(得分:1)

您需要Windows SDK中的DLL System.Management.Automation.dll https://stackoverflow.com/a/1187978/169714

相关问题