使用凭证.NET程序启动过程错误(句柄是无效)句柄、凭证、错误、过程

2023-09-03 02:38:57 作者:擦掉眼泪我依然是王

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

I have an Windows Form application that supplies the User Name, Domain, and Password to the StartInfo, and it throws this:

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

System.ComponentModel.Win32Exception: The handle is invalid at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start()

当我让凭据默认为当前用户我没有得到任何这样的错误,而这个过程中我开始工作的范围内,它不需要使用凭证(vim的信任状是必要的在MSBuild的脚本映射驱动器)。这里的code,填补了启动信息:

When I allow the credentials to default to current user I get no such error, and the process I start works to the extent that it doesn't need to use credentials (the creds are necessary for mapping a drive in an MSBuild script). Here's the code that fills the start info:

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,例如,它捕获它。因此,这种无效的句柄的事情发生了CRED通过后。什么我可能会被省略或搞砸任何想法?

It isn't that the user/psw isn't matching, because when I provide a bad psw, for example, it catches it. So, this "invalid handle" thing is happening after the cred is passed. Any ideas on what I might be omitting or screwing up?

推荐答案

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

You have to redirect your Input, Error, and Output.

例如:

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);
 }

也是微软曾表示,其误差应阅读,无法重定向输入。 (曾经有一个链接,但不再工作)

Also microsoft has said the error should read, "Unable to redirect input." (used to have a link, but that no longer worked)