2014-10-20 27 views
0

我目前工作的一个项目,我叫用户提取物的基础上,我的域名包含从我的Active Directory用户的列表框中,当选择某个用户时,我得到该用户的所有信息,例如姓名,电子邮件,电话等,但活动目录中的某些值以整数8的值存储,我可以使用GetDirectoryEntry()。仅当该值是活动目录中的字符串时才在文本框中显示所有信息。从下面的代码中,主目录,主驱动器,创建时,更改时都工作,因为他们是格式广义时间或字符串,但帐户过期,最后注销,密码错误时间不工作,因为他们的类型是整数8我尝试:查询通过Active Directory,坚持用能类型“诠释”不能隐式转换为“字符串”

 if (rs.GetDirectoryEntry().Properties["lastLogonTimestamp"].Value != null) 
      lastlogon.Text = rs.GetDirectoryEntry().Properties["lastLogonTimestamp"].Value.ToString(); 
      int x = Int32.Parse(lastlogon.Text); 
      lastlogon.Text = x; 

的错误,我得到:

Error 1 Cannot implicitly convert type 'int' to 'string' 

任何帮助非常感谢,谢谢你们!!!!

这里是我使用的代码:

private void ShowUserInformation(SearchResult rs) 
     if (rs.GetDirectoryEntry().Properties["accountExpires"].Value != null) 
      accountexpires.Text = rs.GetDirectoryEntry().Properties["accountExpires"].Value.ToString(); 

     if (rs.GetDirectoryEntry().Properties["homeDrive"].Value != null) 
      homedrive.Text = rs.GetDirectoryEntry().Properties["homeDrive"].Value.ToString(); 

     if (rs.GetDirectoryEntry().Properties["homeDirectory"].Value != null) 
      homedirectory.Text = rs.GetDirectoryEntry().Properties["homeDirectory"].Value.ToString(); 

     if (rs.GetDirectoryEntry().Properties["whenCreated"].Value != null) 
      WhenCreate.Text = rs.GetDirectoryEntry().Properties["whenCreated"].Value.ToString();  

     if (rs.GetDirectoryEntry().Properties["lastLogoff"].Value != null) 
      lastloggedoff.Text = rs.GetDirectoryEntry().Properties["lastLogoff"].Value.ToString(); 

     if (rs.GetDirectoryEntry().Properties["badPasswordTime"].Value != null) 
      badpassword.Text = rs.GetDirectoryEntry().Properties["badPasswordTime"].Value.ToString(); 

the following works thanks to DJ Kraze

private void showuserinfo() 
{ 
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
    UserPrincipal qbeUser = new UserPrincipal(ctx); 
    PrincipalSearcher srch = new PrincipalSearcher(qbeUser); 
    foreach (var found in srch.FindAll()) 
    { 
     try 
     { 
      UserPrincipal foundUser = found as UserPrincipal; 
      if (foundUser != null) 
      { 
       foundUser.IsAccountLockedOut(); 
       lastlogon.Text = (foundUser.LastLogon).ToString(); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
} 
+4

像'lastlogon.Text = x.ToString();' – Habib 2014-10-20 15:49:59

+0

'lastlogon.Text = x'调用'ToString'你的错误信息告诉你到底发生了什么事情..一个简单的建议是使用调试器和通过代码一步,你会看到,当它在该行上的错误..它基本上说,例如文本框的文本字段不能容纳一个整数值..将其转换为字符串或使用'lastlogon.Text = x.ToString( );' – MethodMan 2014-10-20 16:02:51

+0

@Habib我试过你的解决方案,现在我得到这个错误:输入字符串没有在一个正确的格式。 – 2014-10-20 16:21:09

回答

2
if (rs.GetDirectoryEntry().Properties["lastLogonTimestamp"].Value != null) 
      lastlogon.Text = rs.GetDirectoryEntry().Properties["lastLogonTimestamp"].Value.ToString(); 
      int x = Int32.Parse(lastlogon.Text); 
      lastlogon.Text = x.ToString(); 

WATCHOUT是xint,而不是一个string,所以你需要转换其分配给lastlogon.Text前的财产。

+0

感谢您的及时回复,但即使有铸造,我不得不与实际上从活动目录,未来在数据问题作为“System .__ ComObject”,所以根据DJ Kraze的建议,我使用了PrincipalContext,UserPrincipal,Principal搜索器,并能够成功检索所有整数8值并将其设置为我的文本框。 – 2014-10-20 22:09:31

相关问题