作为系统帐户运行的 Windows 服务如何获取当前用户的 \AppData\Local\ 特殊文件夹?



我的应用程序是一个windows桌面exe, windows服务在System帐户下运行。我的windows服务需要与用户也将安装的第三方应用程序集成,该应用程序将一些配置信息存储在windows特殊文件夹之一的ini文件中:C:Users[UserName]AppDataLocal[第三方应用程序名称]

当Windows服务作为系统帐户运行时,我的Windows服务如何检索当前用户的路径到该文件夹中读取ini文件?

理想情况下,我会使用像

这样的东西
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

返回正确的AppDataLocal

但是因为我的windows服务是作为SYSTEM运行的(这是不能改变的),这个方法反而返回:当地C:Windowssystem32config systemprofile AppData

那么我的windows服务如何获得当前登录在用户的LocalApplicationData特殊文件夹?

下面的代码显示了如何获取登录用户列表。一旦您有了登录用户的列表,那么您就可以检查您感兴趣的文件(file . getlastwritetime)的最后更新时间。

添加引用:系统。管理

创建类(名字: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;
}

使用:

List<UserInfo> users = GetLoggedInUserInfo();

:

<
  • Win32_UserAccount类/gh><
  • Win32_UserProfile类/gh><
  • ManagementObjectSearcher类/gh><
  • ManagementObject类/gh>
  • 文件。

相关内容

最新更新