【C#】C#运行bat脚本

标签: C# bat脚本

对于.net桌面端开发者而已,使用bat脚本有时候会对开发有很大的便利


今天就来说一下在.net中怎么去运行一个bat脚本吧


直接放代码:


        public string RunCmd(string path)
        {
            string str;
            Process p = new Process();
            p.StartInfo.FileName = path;         //确定程序名
            p.StartInfo.UseShellExecute = false;        //是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;   //接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;   //重定向标准错误输出
            p.StartInfo.CreateNoWindow = false;          //不显示程序窗口
            p.Start();//启动程序
                      //向cmd窗口写入命令
                      //  p.StandardInput.WriteLine(data);
                      //  p.StandardInput.AutoFlush = true;
                      //获取cmd窗口的输出信息
            str = p.StandardOutput.ReadToEnd();
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();
            return str;//返回bat脚本输出
        }


本文出自《粉墨记忆》 => 《【C#】C#运行bat脚本
转载时请注明出处及相应链接, 本文地址:https://www.fmxk.ac.cn/?post=16

WRITTEN BY

avatar


发表评论: