需要 using System.Diagnostics;
using System.IO;
using System.IO;
一。方法1
public static string RunCommand(string command)
{
//實例一個Process類,啟動一個獨立進程
Process p = new Process();
public static string RunCommand(string command)
{
//實例一個Process類,啟動一個獨立進程
Process p = new Process();
//Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:
p.StartInfo.FileName = "cmd.exe"; //設定程序名
p.StartInfo.Arguments = "/c " + command; //設定程式執行參數
p.StartInfo.UseShellExecute = false; //關閉Shell的使用
p.StartInfo.Arguments = "/c " + command; //設定程式執行參數
p.StartInfo.UseShellExecute = false; //關閉Shell的使用

这篇博客详细介绍了如何在C#中执行批处理命令。通过两种不同的方法,展示了如何利用Process类来运行cmd命令并获取输出结果。示例包括运行bat文件、ping命令、复制文件以及系统关机等操作。
最低0.47元/天 解锁文章
1109

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



