【问题标题】:.NET Process Start Process Error using credentials (The handle is invalid).NET Process Start Process Error using credentials(句柄无效)
【发布时间】:2010-10-12 07:23:02
【问题描述】:

我有一个 Windows 窗体应用程序,它向 StartInfo 提供用户名、域和密码,它会抛出以下内容:

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

当我允许将凭据默认为当前用户时,我不会收到此类错误,并且我启动的过程可以在不需要使用凭据的范围内工作(凭据对于在 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 时,它会捕获它。所以,这个“无效句柄”的事情是在信用通过之后发生的。关于我可能遗漏或搞砸的任何想法?

【问题讨论】:

    标签: c# .net system.diagnostics


    【解决方案1】:

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

    例如:

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

    微软还表示该错误应为“无法重定向输入”。 (以前有链接,现在失效了)

    【讨论】:

    • 哇,是的。我已经将 RedirectStandardOutput 设置为 true,但没有设置其他两个重定向。我将其他两个设置为 true 并修复了它!谢谢,@Chris Lively!
    • 为此干杯 - 我玩得很开心。
    • 链接关闭 3 年,您的帖子仍然有帮助。推+1。谢谢!
    • 非常感谢,伙计!!!您的回答在 2014 年帮助了我。我在圈子里跑,不知道这个“无效句柄”实际上是什么意思。实际上我试图在这个过程中打印一些东西,它让我走上了错误的轨道 - 浏览了很多帖子,因为打印机驱动程序等存在问题,所以出现了这条消息。这是非常令人困惑的消息,看起来它可能出现在几十个不同的情况意味着完全不同的事情。
    • 你在 2017 年节省了我的时间!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多