Reputation: 13649
Hi All,
How can I programatically get the Computer description? I'm using C# and .NET 2.0.
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.
Upvotes: 1
Views: 5396
Reputation: 53
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;
}
}
}
Upvotes: 2
Reputation: 47680
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);
Upvotes: 8
Reputation: 6057
You need the DLL from the windows SDK
System.Management.Automation.dll
https://stackoverflow.com/a/1187978/169714
Upvotes: 1