在Windows中写入正在运行的进程的stdin

Sar*_*ara 6 c# python stdin cmd process

我想stdin从windows 中的外部进程向现有进程写入数据,并发现linux的类似问题:

如何从外部进程向现有进程的STDIN写入数据?

如何将数据从Python中的不同本地/远程进程流式传输到程序的STDIN中?

https://serverfault.com/questions/443297/write-to-stdin-of-a-running-process-using-pipe

等等,但现在我想知道如何在Windows中这样做?
我尝试使用此代码,但我收到错误!
我也尝试运行程序并使用此鳕鱼发送标准输入但再次出错!

在CMD中:

type my_input_string | app.exe -start
my_input_string | app.exe -start
app.exe -start < pas.txt
Run Code Online (Sandbox Code Playgroud)

在python中:

p = subprocess.Popen('"C:\app.exe" -start', stdin=subprocess.PIPE, universal_newlines=True, shell=True)  
grep_stdout = p.communicate(input='my_input_string')[0]
Run Code Online (Sandbox Code Playgroud)

错误是这样的:

ReadConsole()失败:句柄无效.

在C#中:

try
{
    var startInfo = new ProcessStartInfo();
    startInfo.RedirectStandardInput = true;
    startInfo.FileName = textBox1.Text;
    startInfo.Arguments = textBox2.Text;
    startInfo.UseShellExecute = false;

    var process = new Process();
    process.StartInfo = startInfo;
    process.Start();
    Thread.Sleep(1000);
    var streamWriter = process.StandardInput;
    streamWriter.WriteLine("1");
}
catch (Exception ex)
{ 
    textBox4.Text = ex.Message+"\r\n"+ex.Source;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 在C#中使用该代码stdin(命令行应用程序以新进程启动)崩溃了!但在C#应用程序中我没有任何异常!
我认为那也是因为ReadConsole()
当我使用App.exe从而如果不在后台运行应用程序我可以找到进程和使用从UseShellExecute = false发送到那sendkeys但但这不是一个好主意,因为用户从GUI使用时看到命令行!

如何my_input_string只用CMD 发送没有错误或在python或C#中创建脚本!
有什么想法???

亲切的问候.

wil*_*ien 6

如果您要启动,然后从 c# 提供输入,您可以执行以下操作:

var startInfo = new ProcessStartInfo("path/to/executable");
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;

var process = new Process();
process.StartInfo = startInfo;
process.Start();

var streamWriter = process.StandardInput;
streamWriter.WriteLine("I'm supplying input!");
Run Code Online (Sandbox Code Playgroud)

如果您需要写入已经运行的应用程序的标准输入,我怀疑使用 .net 类很容易做到这一点,因为 Process 类不会给你StandardInput(它会抛出一个InvalidOperationException

编辑:添加参数ProcessStartInfo()