Search code examples
c#wpfauthenticationappdata

AppData folder of user who runs application


I'm currently using this:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

to access the AppData folder of the user logged in. The result is a path like this:

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

But: To run the program on another user, I start a new Process like this:

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.
}

and kill the current one.

So what I want is the new AppData folder but the AppData call results in the default one:

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

What I need is the ApplicationData of the user of the thread my program is working in. AND I don't like to use something like Substring (Only if I have to :)


Solution

  • You need to set LoadUserProfile = true in your ProcessStartInfo otherwise the users profile is not available:

    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
    };