有一个简单的方法来做到这一点,我不能相信没有。 我已经通过networking扫描,发现像20个不同的方法来find当前用户在哪个域,但没有获得当前机器的域(或工作组)。
在非托pipe的c + +这是通过检索:
WKSTA_INFO_100 *buf; NetWkstaGetInfo(NULL, 100, (LPBYTE*)buf); domain_name = pBuf->wki100_langroup;
有人可以帮助我,如果有一种方式获得相同的信息托pipeC#本地?
编辑1: 伙计们,请阅读这个问题。 我不是在寻找用户的域名。
要获取正在运行程序的系统的当前域,可以使用System.DirectoryServices.ActiveDirectory.Domain 。
Domain domain = Domain.GetComputerDomain(); Console.WriteLine( domain.Name );
我参与一个项目,用户可以在任何地方; 域计算机上的非域用户,非域计算机上的用户,没有直接连接到第三方网络上的域等等,所以取决于AD,已经是非启动器。
System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()。在所有这些条件下, DomainName更可靠。
http://blogs.msdn.com/b/trobbins/archive/2006/01/04/509347.aspx
https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.domainname(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet -2
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
使用GetCurrentDomain与Environment.UserDomainName相同,如果您的程序作为非域用户在域计算机上运行,则该工作不正确。 我已经使用了下面的代码:
try { return System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name; } catch (Exception) { return Environment.UserDomainName; }
System.Environment.UserDomainName
如果您不想向System.DirectoryServices添加依赖项,则也可以直接调用NetGetJoinInformation API。