The code below shows how to get a list of logged in users. Once you have a list of logged in users, then you can check the last updated time of the file (File.GetLastWriteTime) that you're interested in.
Add Reference: System.Management
Create a class (name: UserInfo.cs)
UserInfo.cs:
public class UserInfo : IComparable<UserInfo>
{
public string Caption { get; set; }
public string DesktopName { get; set; }
public string Domain { get; set; }
public bool IsLocalAccount { get; set; } = false;
public bool IsLoggedIn { get; set; } = false;
public bool IsRoamingConfigured { get; set; } = false;
public DateTime LastUploadTime { get; set; }
public DateTime LastUseTime { get; set; }
public string LocalPath { get; set; }
public bool IsProfileLoaded { get; set; } = false;
public string ProfilePath { get; set; }
public string SID { get; set; }
public uint SIDType { get; set; }
public string Status { get; set; }
public string Username { get; set; }
public int CompareTo(UserInfo other)
{
//sort by name
if (this.Caption == other.Caption)
return 0;
else if (String.Compare(this.Caption, other.Caption) > 1)
return 1;
else
return -1;
}
public override string ToString()
{
string output = string.Empty;
output += $"Caption: '{Caption}'{Environment.NewLine}";
output += $"Domain: '{Domain}'{Environment.NewLine}";
output += $"Username: '{Username}'{Environment.NewLine}";
output += $"IsProfileLoaded: '{IsProfileLoaded}'{Environment.NewLine}";
output += $"IsRoamingConfigured: '{IsRoamingConfigured}'{Environment.NewLine}";
output += $"LocalPath: '{LocalPath}'{Environment.NewLine}";
output += $"LastUseTime: '{LastUseTime.ToString("yyyy/MM/dd HH:mm:ss")}'{Environment.NewLine}";
output += $"SID: '{SID}'{Environment.NewLine}";
return output;
}
}
GetLoggedInUserInfo:
public List<UserInfo> GetLoggedInUserInfo()
{
List<UserInfo> users = new List<UserInfo>();
//create reference
System.Globalization.CultureInfo cultureInfo = System.Globalization.CultureInfo.CurrentCulture;
using (ManagementObjectSearcher searcherUserAccount = new ManagementObjectSearcher("SELECT Caption, Domain, LocalAccount, Name, SID, SIDType, Status FROM Win32_UserAccount WHERE Disabled = false"))
{
foreach (ManagementObject objUserAccount in searcherUserAccount.Get())
{
if (objUserAccount == null)
continue;
//create new instance
UserInfo userInfo = new UserInfo();
string caption = objUserAccount["Caption"].ToString();
//set value
userInfo.Caption = caption;
userInfo.Domain = objUserAccount["Domain"].ToString();
userInfo.IsLocalAccount = (bool)objUserAccount["LocalAccount"];
userInfo.Username = objUserAccount["Name"].ToString();
string sid = objUserAccount["SID"].ToString();
userInfo.SID = sid;
userInfo.SIDType = Convert.ToUInt32(objUserAccount["SIDType"]);
userInfo.Status = objUserAccount["Status"].ToString();
using (ManagementObjectSearcher searcherUserProfile = new ManagementObjectSearcher($"SELECT LastUseTime, LastUploadTime, Loaded, LocalPath, RoamingConfigured FROM Win32_UserProfile WHERE SID = '{sid}'"))
{
foreach (ManagementObject objUserProfile in searcherUserProfile.Get())
{
if (objUserProfile == null)
continue;
if (objUserProfile["LastUploadTime"] != null)
{
string lastUploadTimeStr = objUserProfile["LastUploadTime"].ToString();
if (lastUploadTimeStr.Contains("."))
lastUploadTimeStr = lastUploadTimeStr.Substring(0, lastUploadTimeStr.IndexOf("."));
DateTime lastUploadTime = DateTime.MinValue;
//convert DateTime
if (DateTime.TryParseExact(lastUploadTimeStr, "yyyyMMddHHmmss", cultureInfo, System.Globalization.DateTimeStyles.AssumeUniversal, out lastUploadTime))
userInfo.LastUseTime = lastUploadTime;
//set value
userInfo.LastUploadTime = lastUploadTime;
}
string lastUseTimeStr = objUserProfile["LastUseTime"].ToString();
if (lastUseTimeStr.Contains("."))
lastUseTimeStr = lastUseTimeStr.Substring(0, lastUseTimeStr.IndexOf("."));
DateTime lastUseTime = DateTime.MinValue;
//convert DateTime
if (DateTime.TryParseExact(lastUseTimeStr, "yyyyMMddHHmmss", cultureInfo, System.Globalization.DateTimeStyles.AssumeUniversal, out lastUseTime))
userInfo.LastUseTime = lastUseTime;
//Debug.WriteLine($"LastUseTime: '{objUserProfile["LastUseTime"].ToString()}' After Conversion: '{lastUseTime.ToString("yyyy/MM/dd HH:mm:ss")}'");
userInfo.LocalPath = objUserProfile["LocalPath"].ToString();
userInfo.IsProfileLoaded = (bool)objUserProfile["Loaded"];
userInfo.IsRoamingConfigured = (bool)objUserProfile["RoamingConfigured"];
}
}
if (userInfo.IsProfileLoaded)
{
Debug.WriteLine(userInfo.ToString());
//add
users.Add(userInfo);
}
}
}
//sort by LastUseTime
users.Sort(delegate (UserInfo info1, UserInfo info2) { return info1.LastUseTime.CompareTo(info2.LastUseTime); });
return users;
}
Usage:
List<UserInfo> users = GetLoggedInUserInfo();
Resources: