Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用UserPrincipal对象查找机器名?_C#_C# 4.0 - Fatal编程技术网

C# 如何使用UserPrincipal对象查找机器名?

C# 如何使用UserPrincipal对象查找机器名?,c#,c#-4.0,C#,C# 4.0,我得到了UserPrincipal类对象,想找到机器名、ip地址或mac地址。 任何一个。 有可能这样做吗?如果有,怎么做? 如果我要求的是任何不可能的含糊不清的事情,请道歉。试试这个: string sysname = Environment.MachineName; string sysname = System.Net.Dns.GetHostName(); string sysname = System.Windows.Forms.SystemInformation.ComputerNam

我得到了
UserPrincipal
类对象,想找到机器名、ip地址或mac地址。 任何一个。 有可能这样做吗?如果有,怎么做? 如果我要求的是任何不可能的含糊不清的事情,请道歉。

试试这个:

string sysname = Environment.MachineName;
string sysname = System.Net.Dns.GetHostName();
string sysname = System.Windows.Forms.SystemInformation.ComputerName;
string sysname = System.Environment.GetEnvironmentVariable("COMPUTERNAME');
也许这就是你的意思:

太来自


要查找主机的名称,请使用

string hostName = System.Net.Dns.GetHostName();
如果要查找主机的IP地址,请使用以下命令:

var address = System.Dns.GetHostEntry(hostName)
     .AddressList
     .FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);

这将为您提供类型为“
InterNetwork
”的第一个地址,即第一个已知的IP地址。当然,可能有多个IP地址链接到主机,在这种情况下,您应该迭代上面看到的
AddressList
属性。

UserPrincipal对象只标识用户,而不是机器(除非您的网络管理员已决定将PC标识为用户)


所以你的问题确实没有标准答案。如果有一个user->pc链接,它可能存储在Active Directory或某个数据库中。请与您的系统管理员联系,了解如何建立该链接。

您是在查找代码运行的机器名,还是要查找链接到特定UserPrincipal的机器名?UserPrincipal表示用户,而不是他们的机器(除非可能是机器帐户)。是的,我知道@MarcGravel,但是一定有办法使用我的userPrinicpal对象找到机器名。这个对象只是一台连接在我的网络域中的机器。@PrakashVishwakarma为什么“必须”?据我所知,机器名根本不是
UserPrincipal
的一个因素。除非帐户本身是一个机器帐户,在这种情况下,它很可能是
;请您澄清一下,您所说的是当前计算机(即代码运行的位置)的名称等,还是某些远程用户正在使用的计算机。这是用户名。。不是计算机名。我想要网络域中的UserPrincipal对象的计算机名。UserPrincipal对象不是我正在使用的本地计算机的名称,它是连接到网络的计算机。所以上面的答案对我不适用。可能这就是你要找的。我想OP是指远程用户的机器名。我在问题中没有看到远程用户的参考…@RoyDictus可能在评论中?
var address = System.Dns.GetHostEntry(hostName)
     .AddressList
     .FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);