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

本文关键字:文件夹 AppData 用户 运行 应用程序 | 更新日期: 2023-09-27 17:59:07

我当前正在使用这个:

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这样的东西(只有在必须使用时:)

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

您需要在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
};