我需要以另一个用户开始一个进程,这是一个轰炸.
我将我的代码缩减为一个简单的参考示例.此代码可以正常启动进程:
var info = new ProcessStartInfo("notepad.exe")
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true
};
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加UserName和Password值:
var info = new ProcessStartInfo("notepad.exe")
{
UserName = "user",
Password = StringToSecureString("password"),
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true
};
Process.Start(info);
Run Code Online (Sandbox Code Playgroud)
它轰炸了一个非常有用的System.ComponentModel.Win32Exception消息:
无法启动该服务,因为它已被禁用,或者因为它没有与之关联的已启用设备.
以防万一,这是安全字符串转换方法:
private static SecureString StringToSecureString(string s)
{
var secure = new SecureString();
foreach (var c in s.ToCharArray())
{
secure.AppendChar(c);
}
return secure;
}
Run Code Online (Sandbox Code Playgroud)
任何想法或替代解决方案将非常感谢!