There is a property in .NET System.Environment.MachineName which read the name of a computer cropped up to the 15 characters because of compatibility with Windows 98.
How can I get full (not cropped) name?
There is a property in .NET System.Environment.MachineName which read the name of a computer cropped up to the 15 characters because of compatibility with Windows 98.
How can I get full (not cropped) name?
I suggest using interop
using System.Runtime.InteropServices;
...
[DllImport("KERNEL32.dll", CharSet = CharSet.Auto, BestFitMapping = false)]
private extern static int GetComputerName(
[Out]StringBuilder nameBuffer,
ref int bufferSize);
...
int size = 0; // do not try to return any name, but its actual size only
// What's actual size of the machine name?
GetComputerName(null, ref size);
// Obtaining the machine name
StringBuilder buffer = new StringBuilder(size);
GetComputerName(buffer, ref size);
string name = buffer.ToString();
You can also use WMI to get the name of the system
using System.Management;
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT Name FROM Win32_ComputerSystem");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_ComputerSystem instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Name: {0}", queryObj["Name"]);
}
}
catch (ManagementException e)
{
// exception handling
}