<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
|
Process p = new Process();
![]() p.StartInfo.FileName = "cmd.exe"; //設定程序名 p.StartInfo.Arguments = "/c " command; //設定程式執行參數 p.StartInfo.UseShellExecute = false; //關閉Shell的使用 p.StartInfo.RedirectStandardInput = true; //重定向標準輸入 p.StartInfo.RedirectStandardOutput = true; //重定向標準輸出 p.StartInfo.RedirectStandardError = true; //重定向錯誤輸出 p.StartInfo.CreateNoWindow = true; //設置不顯示窗口 p.Start(); //啟動![]() //p.StandardInput.WriteLine(command); //也可以用這種方式輸入要執行的命令 //p.StandardInput.WriteLine("exit"); //不過要記得加上Exit要不然下一行程式執行的時候會當機![]() return p.StandardOutput.ReadToEnd(); //從輸出流取得命令執行結果 |
本文介绍如何使用 C# 通过 Process 类启动 CMD 并执行指定命令,包括设置参数以实现命令输入、输出及错误信息的重定向,并确保整个过程不显示窗口。

1360

被折叠的 条评论
为什么被折叠?



