溫馨提示×

c#中dos命令參數如何設置

c#
小樊
89
2024-12-13 01:41:06
欄目: 編程語言

在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參數。你可以根據需要修改命令和參數。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女