运行应用程序的用户的 AppData 文件夹

AppData folder of user who runs application

我目前正在使用这个:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

访问登录用户的AppData文件夹,结果是这样的路径:

"C:\Documents and Settings\Michael\Application Data"

但是:为了运行另一个用户上的程序,我开始一个新的进程是这样​​的:

try {    
    var processInfo = new ProcessStartInfo() {
        FileName = System.Reflection.Assembly.GetExecutingAssembly().Location,
        UserName = txtWinLoginUsername.Text,
        Password = txtWinLoginPassword.SecurePassword,
        Domain = this.domain,
        UseShellExecute = false, //kein Plan
    };
    //start program
    Process.Start(processInfo); //execute
    Application.Current.MainWindow.Close(); //close current Window if it worked
} catch {
    //Windows login failed //reset PasswordBox etc.
}

并杀死当前的。

所以我想要的是新的 AppData 文件夹,但 AppData 调用的结果是默认文件夹:

"C:\Documents and Settings\Default\Application Data"

我需要的 是我的程序正在运行的线程的用户的 ApplicationData。而且我不喜欢使用像 Substring 这样的东西(只有当我必须:)

您需要在 ProcessStartInfo 中设置 LoadUserProfile = true 否则用户个人资料不可用:

var processInfo = new ProcessStartInfo
{
    FileName = System.Reflection.Assembly.GetExecutingAssembly().Location,
    UserName = txtWinLoginUsername.Text,
    Password = txtWinLoginPassword.SecurePassword,
    Domain = this.domain,
    UseShellExecute = false, //kein Plan
    LoadUserProfile = true
    //^^^^^^^^^^^^^^^^^^^^
    //Add this line
};