我目前正在使用这个:
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这样的东西(仅当我必须:)< / p>
答案 0 :(得分:1)
您需要在LoadUserProfile = true
中设置ProcessStartInfo
,否则用户个人资料不可用:
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
};