c# - 当 ProcessStartInfo.CreateNoWindow 设置为 true 时,为什么 RedirectStandardInput 没有效果?

标签 c# .net io console finalbuilder

我使用 System.Diagnostics.Process 与已重定向标准输入/输出/错误的第三方控制台应用程序交互 (外部程序是用 C++ 编写的,我无法控制它)。

ProcessStartInfo info = new ProcessStartInfo(fileName, arg); 
info.CreateNoWindow = false; // <- if true, stdin writes don't make it through
info.UseShellExecute = false;
info.RedirectStandardInput  = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError  = true;

var proc = new Process() { StartInfo = info };
proc.OutputDataReceived += new DataReceivedEventHandler(myOutputHandler);
proc.ErrorDataReceived  += new DataReceivedEventHandler(myErrorHandler);
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();

稍后...

proc.StandardInput.WriteLine("some-short-command");

当 info.CreateNoWindow = false 时,在测试控制台应用程序中工作正常; 但当 info.CreateNoWindow = true 时无效;

输出和错误重定向在这两种情况下都能正常工作。

以上代码是为 FinalBuilder 定义自定义操作的类库的一部分。 可以从测试控制台应用程序或从 FinalBuilder 桌面应用程序中运行来观察所描述的行为。

有趣的是,当从第三个上下文 - FinalBuilder 服务器 - 以相同的用户和环境运行时,StandardInput.WriteLine 无效,无论 info.CreateNoWindow 是真还是假。

这是怎么回事?

无论执行上下文如何,我都可以使标准输入重定向工作吗?

最佳答案

不知道为什么,但在这里明确指定用户可以解决问题:

proc.UserName = user;
proc.Domain= domain;
proc.Password= password;

不是很优雅,但对我有用,可能会帮助别人。

关于c# - 当 ProcessStartInfo.CreateNoWindow 设置为 true 时,为什么 RedirectStandardInput 没有效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17777891/

相关文章:

c# - 将 javascript 函数转换为 C#(转换问题)

c# - 如何在本地托管 ASP.NET 应用程序

c++ - 将敏感数据存储在 C++ 编译的二进制文件中是否安全?

c - 如何读取 C 中以空格分隔的 0-9 数字?

sockets - 套接字操作是否在系统级并行执行?

c# - ASP.NET 4.6 MVC 如何返回包含具有正确时区的日期时间数据的 Json 结果?

javascript - C# Web 浏览器支持 javascript 文件 API

c# - 如何在 rdlc 中的表标题顶部而不是左侧显示按列分组

c# - IDbConnection 和 Firebird

c++ - C++-对 `__gxx_personality_seh0'的 undefined reference