Windows窗体应用程序C#中的控制台应用程序

编程入门 行业动态 更新时间:2024-10-28 16:20:09
本文介绍了Windows窗体应用程序C#中的控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有2个申请。 其中一个是控制台应用程序,另一个是普通形式的应用程序-两者都是用C#编写的。我想从Windows窗体应用程序打开(从视图中隐藏)控制台应用程序,并能够将命令行发送到控制台应用程序。

I have 2 applications. One of them is console application, the other is normal form application - both written in C#. I want to open (hidden from view) the console application form the windows form application and be able to send a command lines to the console application.

我该怎么做?

推荐答案

您可以启动后台进程

ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "Myapplication.exe"; startInfo.WindowStyle = ProcessWindowStyle.Hidden; Process process = new Process(); process.StartInfo = startInfo; process.Start();

之后,使用 Process.StandardOutput 属性

and after that use the Process.StandardOutput property

// This is the code for the base process Process myProcess = new Process(); // Start a new instance of this program but specify the 'spawned' version. ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(args[0], "spawn"); myProcessStartInfo.UseShellExecute = false; myProcessStartInfo.RedirectStandardOutput = true; myProcess.StartInfo = myProcessStartInfo; myProcess.Start(); StreamReader myStreamReader = myProcess.StandardOutput; // Read the standard output of the spawned process. string myString = myStreamReader.ReadLine(); Console.WriteLine(myString); myProcess.WaitForExit(); myProcess.Close();

如果要向该过程发送命令,只需使用 Process.StandardInput 属性

If you want to send commands to this process, just use Process.StandardInput Property

// Start the Sort.exe process with redirected input. // Use the sort command to sort the input text. Process myProcess = new Process(); myProcess.StartInfo.FileName = "Sort.exe"; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardInput = true; myProcess.Start(); StreamWriter myStreamWriter = myProcess.StandardInput; // Prompt the user for input text lines to sort. // Write each line to the StandardInput stream of // the sort command. String inputText; int numLines = 0; do { Console.WriteLine("Enter a line of text (or press the Enter key to stop):"); inputText = Console.ReadLine(); if (inputText.Length > 0) { numLines ++; myStreamWriter.WriteLine(inputText); } } while (inputText.Length != 0);

更多推荐

Windows窗体应用程序C#中的控制台应用程序

本文发布于:2023-11-09 13:40:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572510.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   窗体   控制台   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!