Run Command Prompt Commands

前端 未结 14 1916
暖寄归人
暖寄归人 2020-11-21 05:32

Is there any way to run command prompt commands from within a C# application? If so how would I do the following:

copy /b Image1.jpg + Archive.rar Image2.jp         


        
14条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 06:06

    this is all you have to do run shell commands from C#

    string strCmdText;
    strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
    System.Diagnostics.Process.Start("CMD.exe",strCmdText);
    

    EDIT:

    This is to hide the cmd window.

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
    process.StartInfo = startInfo;
    process.Start();
    

    EDIT: 2

    Important is that the argument begins with /C otherwise it won't work. How Scott Ferguson said: it "Carries out the command specified by the string and then terminates."

提交回复
热议问题