Process.Start一个控制台应用程序,在其中输入然后在Form中输出

时间:2023-01-26 20:45:39

I would like to open a simple console application that looks like this :

我想打开一个看起来像这样的简单控制台应用程序:

    static void Main(string[] args)
    {
        Console.WriteLine("Beginning");
        string smth = Console.ReadLine();
        Console.WriteLine("End");
    }

I open it thanks to Process.Start in a Form like this :

感谢Process.Start在这样的表单中打开它:

    private void button1_Click(object sender, EventArgs e)
    {
        ProcessStartInfo processInfo = new ProcessStartInfo();
        processInfo.FileName = myexe;
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.RedirectStandardOutput = true;
        processInfo.RedirectStandardInput = true;
        processInfo.StandardOutputEncoding = Encoding.GetEncoding(850);
        Process process = new Process();
        process.StartInfo = processInfo;
        process.Start();           
    }

I managed to read output using a StreamReader and get this into a textbox :

我设法使用StreamReader读取输出并将其转换为文本框:

Beginning
End

But the problem is the program doesn't wait at Console.ReadLine(), like it is ignored. And i don't know how to let the user input from keyboard (better if it's directly in the console).

但问题是程序不会在Console.ReadLine()中等待,就像被忽略一样。我不知道如何让用户从键盘输入(如果它直接在控制台中更好)。

Is there a way to do it ?

有办法吗?

EDIT

编辑

Looks like i wasn't clear enough, sorry.

看起来我不够清楚,抱歉。

I'm trying to make a simple Form, with no control, able to "react" when i scan a barcode.

我正在尝试制作一个简单的Form,无法控制,能够在我扫描条形码时“反应”。

Basically, i want the console application to run background so when my barcode is scanned, i can get it as my output value :

基本上,我希望控制台应用程序运行后台,所以当扫描我的条形码时,我可以将它作为我的输出值:

  1. Form "Please scan your barcode"
  2. 表格“请扫描您的条形码”
  3. Console app is launched
  4. 控制台应用程序已启动
  5. Barcode is scanned
  6. 条形码被扫描
  7. Console app close
  8. 控制台应用关闭
  9. Barcode as output value
  10. 条形码作为输出值

So StandardInput doesn't seems to be the solution for my problem.

所以StandardInput似乎不是我的问题的解决方案。

That's why i'm asking you if there is a way to achieve this.

这就是为什么我问你是否有办法实现这一目标。

1 个解决方案

#1


0  

I am not quite sure what kind of problem you are facing, but I'm noticing that you aren't utilizing the StandardInput and StandardOutput properties of the Process instance (which is what you should be doing after redirecting them). See if the following example helps you.

我不太确定你遇到了什么样的问题,但是我注意到你没有使用Process实例的StandardInput和StandardOutput属性(这是你重定向后应该做的)。看看以下示例是否对您有所帮助。

If you have this console application:

如果你有这个控制台应用程序:

static void Main(string[] args)
{
    Console.WriteLine("Beginning");
    var str = Console.ReadLine();
    Console.WriteLine("End: " + str);
}

And you invoke it through this:

你通过这个调用它:

ProcessStartInfo processInfo = new ProcessStartInfo
{
    FileName = "console_program",
    CreateNoWindow = true,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardInput = true,
};
Process process = new Process
{
    StartInfo = processInfo,
};
process.Start();
process.StandardInput.WriteLine("Hey!");
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

You will get the output value:

您将获得输出值:

"Beginning\r\nEnd: Hey!\r\n"

#1


0  

I am not quite sure what kind of problem you are facing, but I'm noticing that you aren't utilizing the StandardInput and StandardOutput properties of the Process instance (which is what you should be doing after redirecting them). See if the following example helps you.

我不太确定你遇到了什么样的问题,但是我注意到你没有使用Process实例的StandardInput和StandardOutput属性(这是你重定向后应该做的)。看看以下示例是否对您有所帮助。

If you have this console application:

如果你有这个控制台应用程序:

static void Main(string[] args)
{
    Console.WriteLine("Beginning");
    var str = Console.ReadLine();
    Console.WriteLine("End: " + str);
}

And you invoke it through this:

你通过这个调用它:

ProcessStartInfo processInfo = new ProcessStartInfo
{
    FileName = "console_program",
    CreateNoWindow = true,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardInput = true,
};
Process process = new Process
{
    StartInfo = processInfo,
};
process.Start();
process.StandardInput.WriteLine("Hey!");
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

You will get the output value:

您将获得输出值:

"Beginning\r\nEnd: Hey!\r\n"