2

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)返回当前用户文件夹,但在系统帐户下它返回例如:

C:\Windows\system32\config\systemprofile\AppData\Roaming\

那么问题来了,如何通过系统账号获取所有用户的AppData文件夹呢?

4

2 回答 2

0

我认为,这可以帮助你:

//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
于 2013-08-02T12:53:47.940 回答
0

当我想获取“SYSTEM”帐户下的用户特殊文件夹(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);
}

“Disabled = 0”是获取活跃用户的选项。如果您使用“regedit”打开您的注册表,您将找到所需的一切。

于 2015-04-02T07:54:51.673 回答