C#调用CMD并把运行结果输出到winform窗体中
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CmdPerformExe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
try
{
string str = txtText.Text.ToString();
Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe"; //获取或设置要启动的应用程序或文档
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true; //接受来自掉用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;