C#无法获取某些客户端计算机名称 [英] C# fail to get some client computer name

查看:100
本文介绍了C#无法获取某些客户端计算机名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用以下代码获取客户端计算机名称,但是它对某些客户端有效,并且某些客户端会出错

I have used the below code to get client computer name, however it work for some of the clients and some of the client get error

        string IPAdd = Request.UserHostName;
        IPHostEntry hostEntry = Dns.GetHostEntry(IPAdd);
        string compName = hostEntry.HostName; 

下面的图片是错误的:

Below picture is the error:

尚无此类主机.
第33行:字符串IPAdd = Request.UserHostName;
第34行:IPHostEntry hostEntry = Dns.GetHostEntry(IPAdd); <-引发错误

No such host is known.
Line 33: string IPAdd = Request.UserHostName;
Line 34: IPHostEntry hostEntry = Dns.GetHostEntry(IPAdd); <-- Throws error

请帮助,谢谢.

推荐答案

使用此代码

string compName = Environment.MachineName.ToString();

string compName = Environment.MachineName.ToString();

这篇关于C#无法获取某些客户端计算机名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆