.NET System.Environment.MachineName中有一个属性,它读取由于与Windows 98兼容而裁剪的最多15个字符的计算机的名称。
如何获得完整(未裁剪)的名称?
答案 0 :(得分:2)
我建议使用 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();
答案 1 :(得分:1)
您还可以使用WMI获取系统名称
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
}