在程序中啓動CMD執行批處理

 

        /// <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();
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章