C#学习(4)——使用shell调用外部exe应用

时间:2015-04-06 18:42:33   收藏:0   阅读:235

一 首先创建一个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——峰烨

原文:http://www.cnblogs.com/tjufengye/p/4396302.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!