C# 通过Active Directory查询时,无法隐式转换类型';int';至';字符串';
我目前正在从事一个名为user extract的项目,该项目包含一个基于我的域的active directory用户列表框,当选择某个用户时,我会获取该用户的所有信息,如姓名、电子邮件、电话等,但active directory中的一些值存储为整数8值,我能够使用GetDirectoryEntry().Properties并仅当值是active directory中的字符串时才显示文本框上的所有信息。从下面的代码中可以看出,主目录、主驱动器、创建时、更改时都可以工作,因为它们的格式是通用时间或字符串,但帐户过期、上次注销、密码错误时间不工作,因为它们的类型是integer 8,我尝试了:C# 通过Active Directory查询时,无法隐式转换类型';int';至';字符串';,c#,string,active-directory,int,type-conversion,C#,String,Active Directory,Int,Type Conversion,我目前正在从事一个名为user extract的项目,该项目包含一个基于我的域的active directory用户列表框,当选择某个用户时,我会获取该用户的所有信息,如姓名、电子邮件、电话等,但active directory中的一些值存储为整数8值,我能够使用GetDirectoryEntry().Properties并仅当值是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();
以下作品感谢DJ Kraze
请注意,
x
是一个int
而不是字符串,因此在将其分配给lastlogon.Text
的属性之前,需要强制转换该字符串。调用ToString
类似于lastlogon.Text=x.ToString()
lastlogon.Text=x
您的错误消息会准确地告诉您发生了什么。。一个简单的建议是使用调试器并单步执行代码,当它在该行出错时,您将看到。。它基本上是说文本字段(例如textbox)不能包含整数值。。将其转换为字符串或使用lastlogon.Text=x.ToString()代码>@Habib我尝试了您的解决方案,但现在我遇到了这个错误:输入字符串的格式不正确。@DJKRAZE您的想法有效,因此前面消息中类似于foundUser.LastLogon的内容为我提供了值,因此您建议我创建一个私有的void LastLogon(){并将其称为用户选择的索引交换的地方???只对那些疯狂的整数8值执行此操作,字符串很好,一定要喜欢字符串值:)@DJKRAZE非常感谢好友:以下工作-private void showuserinfo(){PrincipalContext ctx=new PrincipalContext(ContextType.Domain);UserPrincipal qbeUser=new UserPrincipal(ctx);PrincipalSearcher srch=new PrincipalSearcher(qbeUser);foreach(在srch.FindAll()中找到的变量){try{UserPrincipal foundUser=作为UserPrincipal找到;if(foundUser!=null){foundUser.IsAccountLockedOut();lastlogon.Text=(foundUser.lastlogon.ToString();}}catch(异常示例){MessageBox.Show(ex.Message);}}}}}}感谢您的及时响应,但是即使使用casting,我也会对实际从active directory传入的数据产生问题,例如“System.\u ComObject”因此,根据DJ Kraze的建议,我使用PrincipalContext、UserPrincipal、Principal searcher,并且能够成功地检索所有Integer 8值并将其设置为我的文本框。
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);
}
}
}
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();