1

Hi All,

How can I programatically get the Computer description? I'm using C# and .NET 2.0.

enter image description here

I tried Console.WriteLine(Dns.GetHostName()); but it echoes the Full computer name instead.

I also used the following code:

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

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

But this seems doesn't work, I got this exception:

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.

yonan2236
  • 13,371
  • 33
  • 95
  • 141

3 Answers3

8

It's in the registry value

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

The simplest way to access it would be:

using Microsoft.Win32;  

string key = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters";
string computerDescription = (string)Registry.GetValue(key, "srvcomment", null);
Eternal21
  • 4,190
  • 2
  • 48
  • 63
Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
  • 1
    Please, please, please, _don't_ do this. This registry key is [technically documented](http://technet.microsoft.com/en-us/library/cc787346(v=ws.10).aspx), but this is _not_ the supported way of querying this information. Use `NetServerGetInfo` with `SERVER_INFO_101` - this will be available via C/C++, Pinvoke, or JNI. In particular, some AV products lock the reg key, but the Net API continues to work. – Nicholas Wilson Aug 21 '13 at 15:05
  • @NicholasWilson NetServerGetInfo seems very involved to just query that, with structures and all PInvoke definitions. – Sedat Kapanoglu Aug 21 '13 at 15:18
  • What's involved about a structure? I admit PInvoke is going to be inconvenient (maybe there is a managed API after all?), but it's less a pain than having it not work when Windows 9 is released or something. Try this: remove all write permissions on the lanmanserver regkey, then update the server comment, and query it using the Net API. Clearly the reg key is being emulated for legacy applications and is no longer the primary place for the information. – Nicholas Wilson Aug 21 '13 at 17:10
  • 2
    @NicholasWilson answering your question: declaring it. I'm not arguing your points, I'm just saying there is a huge gap of practicality between this and the "ideal" solution. Please feel free to add your alternative as an answer with a code sample. – Sedat Kapanoglu Aug 21 '13 at 19:13
2

The code below will obtain the computer description. I didn't test this on .NET 2.0, but the management classes used have been around since v1.1 so it should work.

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

You need the DLL from the windows SDK System.Management.Automation.dll https://stackoverflow.com/a/1187978/169714

Community
  • 1
  • 1
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128