获取域名 [英] Get domain name
问题描述
我的电脑是在域(Active Directory)中,我需要动态获取的域名。我发现在互联网上使用code:
My computer is in a Domain (Active Directory) and I need to get the domain name dynamically. I found the following code on the internet:
SelectQuery query = new SelectQuery("Win32_ComputerSystem");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (ManagementObject mo in searcher.Get())
{
if ((bool)mo["partofdomain"])
{
this.Domain = mo["domain"].ToString();
break;
}
}
}
它的工作原理正是我想要的,返回正是我想要的域名(当我登录的管理员)。如果用户不是域管理员,我有一个拒绝访问
例外。
有谁知道如何让域甚至非域管理员的用户?
Does anybody know how to get the domain even with non-domain administrator users?
注:我发现在互联网上该解决方案 System.Environment.UserDomainName;
但只给了我域名的一部分
NOTE: I have found this solution on Internet System.Environment.UserDomainName;
but it only gives me a part of the domain name.
即。我的域名是: something.domain.com
和用户域名
返回的东西
。
I.e. my domain is: something.domain.com
and the UserDomainName
returns only something
.
推荐答案
为什么要使用WMI?你不能使用标准的.NET功能?
Why are you using WMI? Can't you use the standard .NET functionality?
System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
这篇关于获取域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!