在C#中,你可以使用System.Diagnostics
命名空間中的Process
類來執行DOS命令
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
// 要執行的DOS命令,例如:dir
string command = "dir";
// 創建一個ProcessStartInfo對象,用于存儲命令的屬性
ProcessStartInfo startInfo = new ProcessStartInfo();
// 設置要執行的命令和參數
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + command;
// 設置使用當前用戶的身份運行命令
startInfo.UseShellExecute = false;
// 防止生成新的窗口
startInfo.CreateNoWindow = true;
// 創建一個新的Process對象,并傳入ProcessStartInfo對象
Process process = new Process();
process.StartInfo = startInfo;
// 開始執行命令
process.Start();
// 等待命令執行完成
process.WaitForExit();
}
}
這個示例將執行dir
命令,你可以將其替換為任何其他DOS命令。請注意,這個示例使用了cmd.exe
來執行命令,因為Windows系統默認使用命令提示符來運行DOS命令。