Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
返回当前用户文件夹,但在系统帐户下,它会返回例如:
C:\Windows\system32\config\systemprofile\AppData\Roaming\
所以问题是如何使用系统帐户获取所有用户的 AppData 文件夹?
答案 0 :(得分:1)
我想,这可以帮到你:
//List all the users
List<String> usersList = new List<String>();
ManagementObjectSearcher sidQuery = new ManagementObjectSearcher("SELECT * FROM Win32_Account");
ManagementObjectCollection results = sidQuery.Get();
foreach (ManagementObject result in results)
{
usersList.Add(result["Name"])
}
//Showing all the partitions letters
String[] strDrives = Environment.GetLogicalDrives();
Foreach (string strDrive in strDrives)
{
//Check if profil has AppData folder
List<String> usersAppData = new List<String>();
String pathAppData = String.Empty;
foreach(String user in usersList)
{
pathAppData = String.Format("{0}\Users\{1}\AppData", strDrive, user);
if(Directory.Exists(pathAppData ))
{
usersAppData.Add(pathAppData);
}
}
}
//Here, you've got all the users AppData folder in usersAppData
答案 1 :(得分:0)
当我想要获取&#34; SYSTEM&#34;下的用户特殊文件夹(Desktop或MyDocuments)的路径时,我遇到了同样的问题。帐户。经过长时间的调查,我找到了解决方案:
string sid = string.Empty;
using (var mos = new ManagementObjectSearcher(
"Select SID From Win32_UserAccount where Disabled = 0"))
{
foreach (var bios in mos.Get())
{
sid = bios["SID"].ToString();
}
}
using (RegistryKey key = Registry.Users.OpenSubKey(sid +
@"\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"))
{
var docFolder = key.GetValue("Personal");
var desktopFolder = key.GetValue("Desktop");
Console.WriteLine("document folder: {0}", docFolder);
Console.WriteLine("Desktop folder: {0}", desktopFolder);
}
&#34;禁用= 0&#34;是获得活跃用户的选项。如果您使用&#34; regedit&#34;打开您的注册表你会找到你需要的一切。