c#学习4

下面来学习一下控制台程序与窗体的连接问题。

整体思路如下:

先建一个控制台程序,放到一个路径中,然后再写一个窗体,在代码中实现窗体与控制台之间的连接。

首先需要先写一个控制台程序。例如一个简单的程序如下。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication1

{    

class Program    

{       

  static void Main(string[] args)    

     {            

System.Console.WriteLine("Hello,World");    

     }

    }

}

然后打开vc建一个windows窗体程序。例如,

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.Windows.Forms;

namespace CRC

{   

  public partial class Form1 : Form    

{       

  public Form1()       

  {            

InitializeComponent();      

   }

        private void button1_Click(object sender, EventArgs e)      

   {            

Process cmd = new Process();          

   cmd.StartInfo.FileName = "D:\\visual studio\\workplace\\CRC\\CRC\bin\\Debug\\CRC.exe";  //控制台程序的目录。

   cmd.StartInfo.UseShellExecute = false;            

   cmd.StartInfo.RedirectStandardInput = true;            

   cmd.StartInfo.RedirectStandardOutput = true;

   cmd.StartInfo.CreateNoWindow = true;

   cmd.Start();       

    this.listBox1.Items.Add(cmd.StandardOutput.ReadToEnd());     //将控制台程序的内容返回到窗体的listbox中。  

  }

        private void textBox2_TextChanged(object sender, EventArgs e)     

    {              

   }

        private void textBox1_TextChanged(object sender, EventArgs e)    

     {       

          }

    }

}

图上的组件可由工具箱里的组件拖拽即可。

上面是建立的窗体,这样就基本实现了窗体与控制台程序的连接。

 

转载于:https://www.cnblogs.com/s-j-m/p/4396182.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值