簡體   English   中英

C#使用參數運行cmd並在完成時將其關閉

[英]c# run cmd with argument and close it in finish

System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.FileName = "cmd.exe";
        startInfo.WorkingDirectory = @"C:\";
        startInfo.Arguments = "dir / s / b / o:n / A:D";
        process.StartInfo = startInfo;
        process.Start();

我嘗試使用參數運行cmd,但它不起作用...

看到cmd.exe /? cmd.exe沒有參數dir

正確的語法為cmd.exe /c dir 此外,命令行鍵中還有多余的空間:“ / s”而不是“ / s”,依此類推:

    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = "/c dir /s /b /o:n /A:D";

刪除/和字母之間的spaces ,如下所示:

System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            startInfo.WorkingDirectory = @"C:\";
            startInfo.Arguments = "/c dir /s /b /o:n /A:D";
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();

要等待進程結束,請使用process.WaitForExit();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM