各位: 最近使用C#和C编写了一个应用程序,为了便于查看程序内部信息。打算在界面上点击一个按钮,触发调用cmd。在cmd窗口手动输入命令后,界面解析命令,进而调用不同的C函数,并将函数的printf结果输出到cmd中。 现在遇到点问题,通过Process调用cmd之后,窗口内不能输入,请问是怎么回事。Process调用cmd命令如下: private string RunCmd() { //例Process Process p = new Process(); p.StartInfo.FileN
全部示例代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public static string global_cmd = "dir";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
RunCmd();
}
private string RunCmd()
{
//例Process
Process p = new Process();
p.StartInfo.FileName = "cmd.exe"; //确定程序名
p.StartInfo.UseShellExecute = false; //Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向输入
p.StartInfo.RedirectStandardOutput = true; //重定向输出
p.StartInfo.CreateNoWindow = false; //设置不显示窗口
p.Start();
p.StandardInput.WriteLine();
return p.StandardOutput.ReadToEnd();
}
}
}