VB.NET2010如何获取域用户的displayname,就是中文名称?
Public Function getADLoginName() As String
Dim UserIdentityInfo As System.Security.Principal.WindowsIdentity
Dim strInfo As String
UserIdentityInfo = System.Security.Principal.WindowsIdentity.GetCurrent()
strInfo = UserIdentityInfo.Name
getADLoginName = strInfo
End Function
上面这个代码是以前网上查的,在VB.NET2010可以获取登录用户的登录名称,在VB.NET 2010里以前要如何获取域用户的显示名称呢?就是中文名称(displayname)?谢谢! --------------------编程问答-------------------- 麻烦大家了。 --------------------编程问答-------------------- --------------------编程问答--------------------
{
//實例一個Process類,啟動一個獨立進程
Process p = new Process();
//Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:
p.StartInfo.FileName = "cmd.exe"; //設定程序名
p.StartInfo.Arguments = "/c " + command; //設定程式執行參數
p.StartInfo.UseShellExecute = false; //關閉Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向標準輸入
p.StartInfo.RedirectStandardOutput = true; //重定向標準輸出
p.StartInfo.RedirectStandardError = true; //重定向錯誤輸出
p.StartInfo.CreateNoWindow = true; //設置不顯示窗口
p.Start(); //啟動
//p.StandardInput.WriteLine(command); //也可以用這種方式輸入要執行的命令
//p.StandardInput.WriteLine("exit"); //不過要記得加上Exit要不然下一行程式執行的時候會當機
return p.StandardOutput.ReadToEnd(); //從輸出流取得命令執行結果
}
private static SearchResultCollection _ADHelper(string domainADsPath, string username, string password, string schemaClassNameToSearch)
{
DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot = new DirectoryEntry(domainADsPath,username, password);
searcher.Filter = "(objectClass=" + schemaClassNameToSearch + ")";
searcher.SearchScope = SearchScope.Subtree;
searcher.Sort = new SortOption("name",SortDirection.Ascending);
// If there is a large set to be return ser page size for a paged search
searcher.PageSize = 512;
searcher.PropertiesToLoad.AddRange(new string[] { "name", "Path", "displayname", "samaccountname", "mail" });
SearchResultCollection results = searcher.FindAll();
return results;
//参数domainADsPath是活动目录的域名,使用类似"LDAP://域名"的形式
//参数schemaClassNameToSearch是过滤条件,
// objectClass=user 查询条件是所有的用户(USER)
}
public GetUserList()
{ }
public string [] ListUsers()
{
string path = "LDAP://IP/CN=Users,DC=idm,DC=gad,DC=nec,DC=com,DC=cn";
// IP:ADIP地址
// DC:域例如 sina.com,cn 可以写为 DC=sina,DC=com,DC=cn
// CN:数据对象 指定要获取的内容
return ListUsers(path);
}
public string[] ListUsers(string path)
{
try
{
DirectoryEntry entry = new DirectoryEntry(path);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(objectClass=*)";
searcher.PropertiesToLoad.Clear();
SearchResultCollection searchResultCollection = searcher.FindAll();
return VisitSearchResultCollection(searchResultCollection);
}
catch (Exception ex) { log.Debug(ex.Message); return new string [0]; }
}
//string messageFormat = "key:{0} value:{1} desc:";
public void VisitSearchResultCollection(SearchResultCollection resultCollection)
{
IList<Users> userList = new List<Users>();
foreach(SearchResult result in resultCollection)
{
string userName;
string displayName;
if (result.Properties.Contains("samaccountname"))
{
ResultPropertyValueCollection resultValue = result.Properties["samaccountname"];
if(resultValue!= null && resultValue.Count >0 && resultValue[0] != null )
{
userName = resultValue[0].ToString();
}
}
if (result.Properties.Contains("displayname"))
{
ResultPropertyValueCollection resultValue = result.Properties["displayname"];
if(resultValue!= null && resultValue.Count >0 && resultValue[0] != null )
{
displayName = resultValue[0].ToString();
}
}
userList.Add(new Users(userName,displayName));
}
UploadHHTHistory(userList);
}
补充:.NET技术 , VB.NET