Is there a way in C# to get the time when the current user logged in?
A command called quser in command prompt will list some basic information about current users, including LOGON TIME.
Is there a System property or something I can access in c# which I can get the user's login time from?
I am getting username by Environment.UserName property. 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: