如何以编程方式找出计算机的上次登录时间? [英] How to programatically find out the last login time to a machine?
问题描述
我想a)以编程方式,b)远程找出用户成功登录Windows计算机的最后日期/时间(通过远程桌面或在控制台上).我愿意采用任何典型的Windows语言(C,C#,VB,批处理文件,JScript等),但是任何解决方案都可以.
I would like to a) programatically and b) remotely find out the last date/time that a user successfully logged into a Windows machine (via remote desktop or at the console). I would be willing to take any typical windows language (C, C#, VB, batch files, JScript, etc...) but any solution would be nice.
推荐答案
尝试一下:
public static DateTime? GetLastLogin(string domainName,string userName)
{
PrincipalContext c = new PrincipalContext(ContextType.Domain,domainName);
UserPrincipal uc = UserPrincipal.FindByIdentity(c, userName);
return uc.LastLogon;
}
您将需要使用System.DirectoryServices和 System.DirectoryServices.AccountManagement
You will need to add references to using using System.DirectoryServices and System.DirectoryServices.AccountManagement
您可以通过执行以下操作来获取到特定计算机的上次登录日期:
You might be able to get the last login Datetime to a specific machine by doing something like this:
public static DateTime? GetLastLoginToMachine(string machineName, string userName)
{
PrincipalContext c = new PrincipalContext(ContextType.Machine, machineName);
UserPrincipal uc = UserPrincipal.FindByIdentity(c, userName);
return uc.LastLogon;
}
这篇关于如何以编程方式找出计算机的上次登录时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!