[英]How to get the Windows user's login time?
Is there a way in C# to get the time when the current user logged in? C# 有没有办法获取当前用户登录的时间?
A command called quser in command prompt will list some basic information about current users, including LOGON TIME.命令提示符中名为 quser 的命令将列出有关当前用户的一些基本信息,包括登录时间。
Is there a System property or something I can access in c# which I can get the user's login time from?是否有系统属性或我可以在 c# 中访问的内容,我可以从中获取用户的登录时间?
I am getting username by Environment.UserName property.我通过 Environment.UserName 属性获取用户名。 Need the login time.
需要登录时间。
I've tried this:我试过这个:
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
Console.WriteLine("Login Time: {0}",GetLastLoginToMachine(Environment .MachineName , Environment.UserName));
public static DateTime? GetLastLoginToMachine(string machineName, string userName)
{
PrincipalContext c = new PrincipalContext(ContextType.Machine, machineName);
UserPrincipal uc = UserPrincipal.FindByIdentity(c, userName);
return uc.LastLogon;
}
Got the following errors:得到以下错误:
You can get the LastUserLogon time from the following namespace.您可以从以下命名空间获取 LastUserLogon 时间。
using System.DirectoryServices.AccountManagement;
Try尝试
DateTime? CurrentUserLoggedInTime = UserPrincipal.Current.LastLogon;
You can get the account information as well :您也可以获取帐户信息:
string userName = WindowsIdentity.GetCurrent().Name.Split('\\')[1];
string machineName = WindowsIdentity.GetCurrent().Name.Split('\\')[0];
Make sure you include the reference to System.DirectoryServices.AccountManagement
:确保包含对
System.DirectoryServices.AccountManagement
的引用:
Then you can do this to get the last logon time:然后您可以执行此操作以获取上次登录时间:
using System.DirectoryServices.AccountManagement;
public static DateTime? GetLastLoginToMachine(string machineName, string userName)
{
PrincipalContext c = new PrincipalContext(ContextType.Machine, machineName);
UserPrincipal uc = UserPrincipal.FindByIdentity(c, userName);
return uc.LastLogon;
}
You can query WMI:您可以查询 WMI:
// using System.Management;
private static Dictionary<string, DateTime> getMachineLogonName(string machine)
{
var loggedOnUsers = new Dictionary<string, DateTime>();
ManagementScope scope = new ManagementScope(String.Format(@"\\{0}\root\cimv2", machine));
SelectQuery sessionQuery = new SelectQuery("Win32_LogonSession");
using (ManagementObjectSearcher sessionSearcher = new ManagementObjectSearcher(scope, sessionQuery))
using (ManagementObjectCollection sessionMOs = sessionSearcher.Get())
{
foreach (var sessionMO in sessionMOs)
{
// Interactive sessions
if ((UInt32)sessionMO.Properties["LogonType"].Value == 2)
{
var logonId = (string)sessionMO.Properties["LogonId"].Value;
var startTimeString = (string)sessionMO.Properties["StartTime"].Value;
var startTime = DateTime.ParseExact(startTimeString.Substring(0, 21), "yyyyMMddHHmmss.ffffff", System.Globalization.CultureInfo.InvariantCulture);
WqlObjectQuery userQuery = new WqlObjectQuery(@"ASSOCIATORS OF {Win32_LogonSession.LogonId='" + logonId + @"'} WHERE AssocClass=Win32_LoggedOnUser");
using (var userSearcher = new ManagementObjectSearcher(scope, userQuery))
using (var userMOs = userSearcher.Get())
{
var username = userMOs.OfType<ManagementObject>().Select(u => (string)u.Properties["Name"].Value).FirstOrDefault();
if (!loggedOnUsers.ContainsKey(username))
{
loggedOnUsers.Add(username, startTime);
}
else if(loggedOnUsers[username]> startTime)
{
loggedOnUsers[username] = startTime;
}
}
}
}
}
return loggedOnUsers;
}
Then just call the method with target machine name:然后只需使用目标机器名称调用方法:
var logins = getMachineLogonName(".");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.