C#学习(4)——使用shell调用外部exe应用
一 首先创建一个windows窗体程序,在其中增加一个Button,以及一个TextBox

二 接下来我们首先在命令提示行中调用我们之前就写过的HelloWorld

C:\Users\gao\Desktop\Hello.exe 代表我的Hello生成应用的位置,随后跟着的两个字符串代表Hello要使用的两个参数,之后enter既得到结果
三 接下来就要使用Button将Hello.exe运行结果输出到TextBox里
双击Button,添加代码
private void button1_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
string str = @"C:\Users\gao\Desktop\Hello.exe"+" fengye C#";
process.StandardInput.WriteLine(str);
process.StandardInput.WriteLine("exit");
string sh = process.StandardOutput.ReadToEnd();
process.Close();
this.textBox1.Text = sh;
}
运行结果如下:

Author——峰烨

浙公网安备 33010602011771号