在程序中启动CMD执行批处理

来源:互联网 发布:淘宝店铺怎么下架宝贝 编辑:程序博客网 时间:2024/05/02 01:00

 

        /// <summary>
        /// 启动CMD执行批处理
        /// </summary>
        /// <param name="workingDirectory">工作目录</param>
        /// <param name="cmdLine">执行命令</param>
        public static void StartCmd(string workingDirectory, string cmdLine)
        {
            using (System.Diagnostics.Process p = new System.Diagnostics.Process())
            {
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.WorkingDirectory = workingDirectory;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;

                p.Start();
                p.StandardInput.WriteLine(cmdLine);
                p.StandardInput.WriteLine("exit");
                p.StandardOutput.ReadToEnd();
                p.Close();
            }
        }

原创粉丝点击