.NET 中机器的域名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/508911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Machine's domain name in .NET?
提问by galets
There gotta be an easy way to do this, I can't believe there's none. I have scanned through net and found, like, 20 different methods to find in which domain current user is, but none to get domain (or workgroup) of current machine.
必须有一个简单的方法来做到这一点,我不敢相信没有。我已经通过网络扫描并找到了 20 种不同的方法来查找当前用户所在的域,但没有一种方法可以获取当前机器的域(或工作组)。
In unmanaged c++ this is retrieved by:
在非托管 C++ 中,这是通过以下方式检索的:
WKSTA_INFO_100 *buf;
NetWkstaGetInfo(NULL, 100, (LPBYTE*)buf);
domain_name = pBuf->wki100_langroup;
can someone help me, if there's a way to get same info in managed C# natively?
如果有办法在本地托管 C# 中获取相同的信息,有人可以帮助我吗?
EDIT1: Folks, please read the question. I am NOT looking for user domain name.
EDIT1:伙计们,请阅读问题。我不是在寻找用户域名。
回答by tvanfosson
To get the current domain of the system on which your progam is running you can use System.DirectoryServices.ActiveDirectory.Domain.
要获取运行程序的系统的当前域,您可以使用 System.DirectoryServices.ActiveDirectory.Domain。
Domain domain = Domain.GetComputerDomain();
Console.WriteLine( domain.Name );
回答by jeepwran
I work on a project where users could be anywhere; non-domain users on a domain machine, users on a non-domain machine, not directly connected to the domain on a third party network, etc. so depending on AD is already a non-starter.
我从事一个用户可以在任何地方的项目;域机器上的非域用户,非域机器上的用户,未直接连接到第三方网络上的域等,因此依赖于 AD 已经是一个非启动器。
System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainNameis far more reliable under all of these conditions.
System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName在所有这些条件下都更加可靠。
http://blogs.msdn.com/b/trobbins/archive/2006/01/04/509347.aspx
http://blogs.msdn.com/b/trobbins/archive/2006/01/04/509347.aspx
Imports System.DirectoryServices
Imports System.Net.NetworkInformation
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
MsgBox("Domain: " & ActiveDirectory.Domain.GetComputerDomain.Name)
Catch ex As Exception
MsgBox(ex.GetType.ToString & ": " & ex.Message)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
MsgBox("Domain: " & IPGlobalProperties.GetIPGlobalProperties().DomainName)
Catch ex As Exception
MsgBox(ex.GetType.ToString & ": " & ex.Message)
End Try
End Sub
End Class
回答by Aleksandr
Using GetCurrentDomainis the same as Environment.UserDomainName, which works incorrectly if your program is running on a domain computer as a non-domain user. I've used the following code:
使用GetCurrentDomain与Environment.UserDomainName相同,如果您的程序作为非域用户在域计算机上运行,则它无法正常工作。我使用了以下代码:
try
{
return System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name;
}
catch (Exception)
{
return Environment.UserDomainName;
}
回答by bendewey
System.Environment.UserDomainName
System.Environment.UserDomainName
回答by David
If you don't want to add a dependency to System.DirectoryServices, you can also call the NetGetJoinInformationAPI directly.
如果不想给 System.DirectoryServices 添加依赖,也可以直接调用 NetGetJoinInformationAPI。
回答by Remco
System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain()wraps DsGetDcNamewhich will search the network for a domain controller even if the machine is not part of a domain. (see remarks)
System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain()包装DsGetDcName,即使机器不是域的一部分,它也会在网络中搜索域控制器。(见备注)
As alternative to NetGetJoinInformationyou could use GetComputerNameExwith the COMPUTER_NAME_FORMAT.ComputerNameDnsDomainflag to get the full DNS domain name.
作为NetGetJoinInformation 的替代方法,您可以使用带有COMPUTER_NAME_FORMAT标志的GetComputerNameEx来获取完整的 DNS 域名。.ComputerNameDnsDomain
(If not part of a domain, it still returns true, but the resulting string will be empty.)
(如果不是域的一部分,它仍然返回 true,但结果字符串将为空。)

