我需要以另一个用户的身份启动一个进程,它正在爆炸。
我将我的代码缩减为一个简单的引用示例。此代码可以很好地启动进程本身:
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 消息轰炸:
The service cannot be started, either because it is disabled or because it has no enabled devices associated with it.
以防万一,下面是安全的字符串转换方法:
private static SecureString StringToSecureString(string s)
{
var secure = new SecureString();
foreach (var c in s.ToCharArray())
{
secure.AppendChar(c);
}
return secure;
}
任何想法或替代解决方案将不胜感激!
最佳答案
您的 Secondary Logon
服务是否已启动,我相信这是在不同用户帐户下启动新进程所必需的?
关于c# - 如果使用不同的用户凭据,为什么 Process.Start 会抛出 Win32Exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4135118/