Я нашел решение:)
Я использую Кассия , чтобы найти имя пользователя:
ITerminalServicesManager manager = new TerminalServicesManager();
ITerminalServicesSession session = manager.CurrentSession;
И выбрать всех пользователей , ктосопоставить с session.UserName
и session.DomainName
:
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_Account where Name='" + session.UserName + "' AND Domain='" + session.DomainName + "'");
ManagementObjectCollection Users = mos.Get();
И после найден% appData% с SID пользователя :
// Get first user
ManagementObject[] arr = new ManagementObject[1];
Users.CopyTo(arr, 0);
ManagementObject User = arr[0];
// Found the %appData%
const string regValueLocalAppData = @"AppData";
const string regKeyShellFolders = @"HKEY_USERS\$SID$\Software\Microsoft\Windows\" +
@"CurrentVersion\Explorer\Shell Folders";
var localAppDataPath = Microsoft.Win32.Registry.GetValue(regKeyShellFolders.Replace("$SID$", User["SID"].ToString()), regValueLocalAppData, null) as string;
// Check if %appData% is not empty// Le dossier %appData% existe
if (!string.IsNullOrWhiteSpace(localAppDataPath))
{
PathToRoaming = localAppDataPath;
}
Также возможносоздайте путь и проверьте, существует ли он:
public string GetAppdataPath() {
string PathToRoaming = "";
ITerminalServicesManager manager = new TerminalServicesManager();
ITerminalServicesSession session = manager.CurrentSession;
// Get path to Users directory
Guid UserProfilesGuid = new Guid("0762D272-C50A-4BB0-A382-697DCD729B80");
IntPtr pPath;
SHGetKnownFolderPath(UserProfilesGuid, 0, IntPtr.Zero, out pPath);
string PathToUsers = System.Runtime.InteropServices.Marshal.PtrToStringUni(pPath);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pPath);
// Build path
string PathToRoamingDir = String.Format("{0}\\{1}\\AppData\\Roaming", PathToUsers, session.UserName);
if (Directory.Exists(PathToRoamingDir))
{
PathToRoaming = PathToRoamingDir;
}
}
[System.Runtime.InteropServices.DllImport("shell32.dll")]
static extern int SHGetKnownFolderPath([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);