我需要以另一個用戶的身份啓動一個進程,並進行轟炸。如果使用不同的用戶憑據,Process.Start爲什麼會拋出Win32Exception?
我將它縮小到一個簡單的參考示例。此代碼工作正常啓動過程本身:
var info = new ProcessStartInfo("notepad.exe")
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true
};
不過,如果我添加了UserName
和Password
值:
var info = new ProcessStartInfo("notepad.exe")
{
UserName = "user",
Password = StringToSecureString("password"),
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true
};
Process.Start(info);
它炸彈與以往任何時候都如此有用System.ComponentModel.Win32Exception消息:
服務無法啓動,或者是因爲它被禁用或者因爲它沒有啓用與其關聯的設備。
以防萬一,這裏是安全的字符串轉換方法:
private static SecureString StringToSecureString(string s)
{
var secure = new SecureString();
foreach (var c in s.ToCharArray())
{
secure.AppendChar(c);
}
return secure;
}
任何意見或替代解決方案將非常感謝!
您的代碼爲我工作,所以這個問題是不符合實際的代碼。 – 2010-11-09 15:09:55