使用凭据的.NET进程启动进程错误(句柄无效)

我有一个Windows窗体应用程序,它为StartInfo提供用户名,域和密码,它会抛出:

System.ComponentModel.Win32Exception: 句柄在System.Diagnostics.Process.Start()的System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)中无效

当我允许凭证默认为当前用户时,我没有得到这样的错误,并且我开始的过程工作到不需要使用凭证的程度(在MSBuild脚本中映射驱动器所需的信用)。 这是填充开始信息的代码:

Process p = new Process(); ProcessStartInfo si = new ProcessStartInfo(buildApp, buildArgs); si.WorkingDirectory = msBuildWorkingDir; si.UserName = txtUserName.Text; char[] psw = txtPassword.Text.ToCharArray(); SecureString ss = new SecureString(); for (int x = 0; x < psw.Length; x++) { ss.AppendChar(psw[x]); } si.Password = ss; si.Domain = "ABC"; si.RedirectStandardOutput = true; si.UseShellExecute = false; si.WorkingDirectory = txtWorkingDir.Text; p.StartInfo = si; p.Start(); 

并不是用户/ psw不匹配,因为当我提供一个糟糕的psw时,它会抓住它。 因此,这个“无效句柄”的事情在信用证通过后发生。 关于我可能会忽略或搞砸的任何想法?

您必须重定向输入,错误和输出。

例如:

 ProcessStartInfo info = new ProcessStartInfo("cmd.exe"); info.UseShellExecute = false; info.RedirectStandardInput = true; info.RedirectStandardError = true; info.RedirectStandardOutput = true; info.UserName = dialog.User; using (Process install = Process.Start(info)) { string output = install.StandardOutput.ReadToEnd(); install.WaitForExit(); // Do something with you output data Console.WriteLine(output); } 

微软也表示错误应该是“无法重定向输入”。 (以前有链接,但不再有效)