2

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 :)

H.B.
  • 166,899
  • 29
  • 327
  • 400
Michael1248
  • 150
  • 1
  • 12
  • 1
    Has that user ever logged in to that machine before? If not, then they won't have a folder yet. Also, have you tried setting `LoadUserProfile = true` in your `ProcessStartInfo` object? – DavidG Jul 28 '16 at 13:22
  • `System.IO.Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System)) + "ProgramData"` like "C:\ProgramData" can not be accessed by other user. How can I fix it – Michael1248 Jul 28 '16 at 14:01
  • You are asking different questions now. – DavidG Jul 28 '16 at 14:03
  • I'm sorry, I should thank you ;) – Michael1248 Jul 28 '16 at 14:08

1 Answers1

2

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
};
DavidG
  • 113,891
  • 12
  • 217
  • 223