关闭

C#执行批处理命令

414人阅读 评论(0) 收藏 举报

using System.Diagnostics ;

using System.IO;

private void btnRun_Click(object sender, EventArgs e)
        {
            txtResult.Text = "";
            processCommand("Ping.exe", this.txtAddress.Text);
            processCommand("Ping.exe", this.txtAddress.Text);
        }

        public void processCommand(string commandName, string argument)
        {
            ProcessStartInfo start = new ProcessStartInfo(commandName);//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
            //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
            start.WorkingDirectory = "d:\\360Downloads\\";
            start.Arguments = argument;//设置命令参数
            start.CreateNoWindow = true;//不显示dos命令行窗口
            start.RedirectStandardOutput = true;//
            start.RedirectStandardInput = true;//
            start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
            txtResult.AppendText(start.WorkingDirectory  + "\n");
            Process p = Process.Start(start);
            StreamReader reader = p.StandardOutput;//截取输出流
            string line = reader.ReadLine();//每次读取一行
            while (!reader.EndOfStream)
            {
                txtResult.AppendText(line + "\n");
                line = reader.ReadLine();
            }
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();//关闭进程
            reader.Close();//关闭流
        }

 

 

=================================================================================

        private void button10_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "bat 文件 (*.bat)|*.bat|所有文件 (*.*)|*.*";
            openFileDialog1.DefaultExt = "mp3";
            openFileDialog1.FileName = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Process ps = new Process();
                ps.StartInfo.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
                ps.StartInfo.FileName = openFileDialog1.FileName;
                ps.StartInfo.CreateNoWindow = true;//不显示dos命令行窗口
                ps.Start();
                ps.Close();//关闭进程

            }

        }

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3177998次
    • 积分:40334
    • 等级:
    • 排名:第92名
    • 原创:478篇
    • 转载:2565篇
    • 译文:1篇
    • 评论:242条
    文章分类
    文章存档
    最新评论