.NET System.Environment.MachineName 中有一个属性,它读取由于与 Windows 98 兼容而被裁剪为 15 个字符的计算机名称。
我如何获得完整(未裁剪)的姓名?
我建议使用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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)