Run Command Prompt Commands

前端 未结 14 2367
暖寄归人
暖寄归人 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:16

    Tried @RameshVel solution but I could not pass arguments in my console application. If anyone experiences the same problem here is a solution:

    using System.Diagnostics;
    
    Process cmd = new Process();
    cmd.StartInfo.FileName = "cmd.exe";
    cmd.StartInfo.RedirectStandardInput = true;
    cmd.StartInfo.RedirectStandardOutput = true;
    cmd.StartInfo.CreateNoWindow = true;
    cmd.StartInfo.UseShellExecute = false;
    cmd.Start();
    
    cmd.StandardInput.WriteLine("echo Oscar");
    cmd.StandardInput.Flush();
    cmd.StandardInput.Close();
    cmd.WaitForExit();
    Console.WriteLine(cmd.StandardOutput.ReadToEnd());
    

提交回复
热议问题