我正在创建一个应用程序,以显示用户的上次登录时间戳,创建日期和其他一些信息。如何使lastlogon显示在列表视图中?
我已经在SO以及其他各种网站上尝试了各种解决方案,但似乎都没有效果
这是我用来显示所需信息的代码
lvwListView.Clear();
lvwListView.Columns.Add("Name", 175, HorizontalAlignment.Left);
lvwListView.Columns.Add("LanID", 100, HorizontalAlignment.Left);
lvwListView.Columns.Add("Email", 225, HorizontalAlignment.Left);
lvwListView.Columns.Add("Phone Number", 150, HorizontalAlignment.Left);
lvwListView.Columns.Add("When Created", 150, HorizontalAlignment.Left);
lvwListView.Columns.Add("Last Logon", 150, HorizontalAlignment.Left);
if (results != null)
{
foreach (Principal p in results)
{
itmListItem = new ListViewItem();
itmListItem.Text = p.Name;
itmListItem.SubItems.Add(p.SamAccountName);
if (p.StructuralObjectClass == "user")
{
var uP = (UserPrincipal)p;
if (uP != null)
{
itmListItem.SubItems.Add(uP.EmailAddress);
}
}
var creationDate = string.Empty;
var telephoneNumber = string.Empty;
var lastLogon = string.Empty;
var prop = string.Empty;
var directoryEntry = p.GetUnderlyingObject() as DirectoryEntry;
prop = "whenCreated";
if (directoryEntry.Properties.Contains(prop))
{
creationDate = directoryEntry.Properties[prop].Value.ToString();
}
prop = "telephoneNumber";
if (directoryEntry.Properties.Contains(prop))
{
telephoneNumber = directoryEntry.Properties[prop].Value.ToString();
}
prop = "lastLogon";
if (directoryEntry.Properties.Contains(prop))
{
lastLogon = directoryEntry.Properties[prop].Value.ToString();
}
itmListItem.SubItems.Add(telephoneNumber);
itmListItem.SubItems.Add(creationDate);
itmListItem.SubItems.Add(lastLogon);
lvwListView.Items.Add(itmListItem);
lvwListView.Refresh();
itmListItem = null;
this.Text = Application.ProductName + " (" + groupName + ")";
}
ctx.Dispose();
}
上面的代码完美显示Name
,LanId
,Email
,phone number
和when created
,但对于lastlogon
,它显示{ {1}}。
我不完全理解为什么会显示System._ComObject
而不显示WhenCreated
?
答案 0 :(得分:0)
WhenCreated存储为String (Generalized-Time) type,而Last-Logon存储为Interval syntax,可以转换为IADsLargeInteger。
这是一个示例扩展,它将把它转换成熟悉的System.DateTime
:
public static class ActiveDirectoryExtensions
{
public static DateTime GetLastLogon(this DirectoryEntry entry)
{
var activeDirectoryLargeInteger = (IAdsLargeInteger)entry.Properties["lastLogon"].Value;
return DateTime.FromFileTimeUtc((activeDirectoryLargeInteger.HighPart << 32) + activeDirectoryLargeInteger.LowPart);
}
[ComImport, Guid("9068270b-0939-11d1-8be1-00c04fd8d503"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
internal interface IAdsLargeInteger
{
long HighPart { get; set; }
long LowPart { get; set; }
}
}
用法:
var lastLogon = directoryEntry.GetLastLogon()