加入收藏 | 设为首页 | 会员中心 | 我要投稿 孝感站长网 (https://www.0712zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 系统 > 正文

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

发布时间:2021-02-04 14:48:01 所属栏目:系统 来源:网络整理
导读:我有2个申请. 其中一个是控制台应用程序,另一个是普通的表单应用程序 – 都是用C#编写的.我想从 Windows窗体应用程序打开(从视图中隐藏)控制台应用程序,并能够将命令行发送到控制台应用程序. 我怎样才能做到这一点? 解决方法 您可以开始后台进程 ProcessSta

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

我怎样才能做到这一点?

解决方法

您可以开始后台进程

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

之后使用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 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);

(编辑:孝感站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读