2

Enviroment.MachineName 返回短名称:

public static string GetCurrentMachineName()
{
    return GetMachineName(Environment.MachineName); //returns hostname
}

我有短主机名:shortName和全名:shortName.company.local。所以,当我打电话时,GetCurrentMachineName()我只得到shortName'shortName.company.local'。有什么问题?

PS:
这对我不起作用:例如,我的主机名是hostname1. 而我在当前网络的朋友的主机名名为hostname2. 所以,当我执行这段代码时:

return System.Net.Dns.GetHostEntry("").HostName;

使用 hostname2 它解析hostname2.company.localhostname1我的主机名。

4

2 回答 2

3

使用它Dns.GetHostEntry("").HostName来获取完整的主机名

public static string GetCurrentMachineName()
    {
        return System.Net.Dns.GetHostEntry("").HostName; //returns hostname
    }
于 2016-10-10T08:31:35.447 回答
0

我解决了,我的问题:

  1. 刷新 DNS 缓存

    ipconfig /flushdns

  2. 清除我的hosts档案。

谢谢你的帮助!

于 2016-10-11T10:06:34.610 回答