我想要读取控制台模式子进程的输出。

3

我想更新一个1995年用Pascal或C编写的命令行程序,现在我使用的是C#。我想读取子程序的输出结果,这是否可能?

我尝试了一些方法,但没有成功。它们如下:

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // Redirect the output stream of the child process.
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.FileName = "asdd.exe";
        p.Start();


        logs.AppendText("Timer Started\n");
        timer1.Enabled = true;


    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // write somethingg and read what is the program doing on command line?
        // What is the program printint? etc... 
        // I try this code but not enough for mo. 
        // logs.AppendText("d:" + p.StandardOutput.ReadToEnd()+"\n");

    }

    private void p_Exited(object sender, EventArgs e)
    {
        timer1.Enabled = false;
    }

我可以翻译与Java、C++、C或C#相关的任何想法。

编辑:

例如:

我的asdd.exe程序源代码是

#include<stdio.h>

int main(){
    printf("something\n");
    printf("something, too\n");
}

这个程序使用C#编写,日志richTextBox会被追加:

something
something, too

你能具体说明一下你想要实现什么吗? - TheBoyan
你想从控制台读取文本还是获取传递给程序的参数? - SDReyes
我之前见过这个问题......等等,你是想在命令行上看到程序的内容(源代码或内存或其他什么),就像quine一样,这是你标题所说的,还是想要获取命令行的内容到你的程序中,这是代码片段所建议的? - Ben Voigt
我正在尝试更新一个程序。我正在使用C#设计GUI,并使用旧程序处理一些操作。在这种情况下,我正在从旧程序中读取一些信息。旧程序是在1995年编写的,源代码已经丢失。 - Alexandre Dominos
我想读取程序内容。不想使用argv,也不想向程序发送任何参数。 - Alexandre Dominos
@Alexandre:我已经更新了你的问题,以表达我认为你的意思。程序的内容是它的代码。我想你指的是输出结果 - Ben Voigt
4个回答

1

我尝试过了,但是没有返回任何结果。要么是我无法使用它,要么就是它不适合我。 - Alexandre Dominos

0
这是我通常从单独的进程中读取数据的方式。现在这会阻塞当前线程直到处理完成,因此我使用后台线程。
process.StartInfo.FileName = "program.exe"
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

process.Start();

process.StandardInput.WriteLine(data);

string result = process.StandardOutput.ReadToEnd();

process.WaitForExit();

我尝试了@cadrell0的代码,但它没有起作用。任何在 while 循环中使用 scanf、printf 函数的 c 控制台应用程序都只会出现黑屏。 - Alexandre Dominos
我不使用以下代码来查看该怎么做: process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - Alexandre Dominos
这个其他进程具体是做什么的?如果您要手动调用它,它看起来会是什么样子? - cadrell0

0

ReadToEnd在子程序结束之前不会返回任何数据。如果您想在程序运行时查看输出,可能需要逐行甚至逐个字符地读取。


我尝试使用StandardOutput.ReadLine(),但它没有返回任何字符串。 - Alexandre Dominos
也许这个程序没用,我会尝试任何使用标准输出的程序。然后再返回。 - Alexandre Dominos
我理解了。我会一遍又一遍地尝试,也许有些事情会改变。 - Alexandre Dominos
@Alexandre:你的意思是在命令提示符下输入了 asdd > result.txt,但输出仍然出现在屏幕上? - Ben Voigt
@Alexandre:我认为这意味着程序没有使用标准输出(stdout)。我猜想你的小测试printf应用程序可以轻松重定向到文件中,对吧? - Ben Voigt
显示剩余8条评论

0

在这个链接中,两个程序是彼此通信的,没错。但是这两个程序都是由同一个人编写的。我的问题是,我没有其中一个程序的源代码,并且想要读取我没有源代码的这个程序应该做什么。 - Alexandre Dominos
@Alexandre Dominos:如果我没记错的话,第二个程序只是一个例子,你不需要特别编写子进程。我认为它将使用你设置的管道作为其标准输入和输出。 - Null Set
实际上,我想用C#读取命令行程序内容。我的应用程序是用C#编写的。虽然我还没有写过它,但我将用C#编写它。命令行程序只是一个exe文件。示例程序代表了这个命令行程序。因此,命令行程序只是exe文件,不允许更改源代码。如果你想说“使用管道读取stdin/stdout”,那是个好主意。我会尝试一下。 - Alexandre Dominos
@Alexandre Dominos:我刚刚注意到该页面上有一条评论,指出旧版程序可能会以不同于命令行的方式缓冲其输出。显然,解决方法是启动cmd.exe作为子进程,并使用它来间接启动您想要的实际子进程。 - Null Set

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接