在C#中,你可以使用System.Diagnostics.Process
類來執行DOS命令并設置參數
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
// 要執行的DOS命令,例如:dir
string command = "dir";
// 要傳遞給命令的參數,例如:/w /a
string parameters = "/w /a";
// 創建一個ProcessStartInfo對象,用于存儲命令和參數
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = command; // 設置命令名稱
startInfo.Arguments = parameters; // 設置命令參數
startInfo.RedirectStandardOutput = true; // 將輸出重定向到控制臺
startInfo.UseShellExecute = false; // 不使用系統外殼程序啟動命令
startInfo.CreateNoWindow = true; // 不創建新窗口
// 創建一個新的Process對象,并傳入startInfo
Process process = new Process();
process.StartInfo = startInfo;
// 啟動命令
process.Start();
// 等待命令執行完成
process.WaitForExit();
}
}
在這個示例中,我們執行了dir
命令,并傳遞了/w /a
參數。你可以根據需要修改命令和參數。