Run CMD command without displaying it?

前端 未结 4 983
难免孤独
难免孤独 2021-02-20 09:59

I have created a Process to run command in CMD.

var process = Process.Start(\"CMD.exe\", \"/c apktool d app.apk\");
process.WaitForExit();

How

相关标签:
4条回答
  • 2021-02-20 10:27

    There are several issues with your program, as pointed out in the various comments and answers. I tried to address all of them here.

    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = "apktool";
    
    //join the arguments with a space, this allows you to set "app.apk" to a variable
    psi.Arguments = String.Join(" ", "d", "app.apk");
    
    //leave it to the application, not the OS to launch the file
    psi.UseShellExecute = false;
    
    //choose to not create a window
    psi.CreateNoWindow = true;
    
    //set the window's style to 'hidden'
    psi.WindowStyle = ProcessWindowStyle.Hidden;
    
    var proc = new Process();
    proc.StartInfo = psi;
    proc.Start();
    proc.WaitForExit();
    

    The main issues:

    • using cmd /c when not necessary
    • starting the app without setting the properties for hiding it
    0 讨论(0)
  • 2021-02-20 10:34

    Try this :

         proc.StartInfo.CreateNoWindow = true;
         proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
         proc.WaitForExit(); 
    
    0 讨论(0)
  • 2021-02-20 10:37
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = true;
    startInfo.UseShellExecute = false;
    startInfo.FileName = "dcm2jpg.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;
    
    0 讨论(0)
  • 2021-02-20 10:46

    You can use the WindowsStyle-Property to indicate whether the process is started in a window that is maximized, minimized, normal (neither maximized nor minimized), or not visible

    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    

    Source: Property:MSDN Enumartion: MSDN

    And change your code to this, becaeuse you started the process when initializing the object, so the properties (who got set after starting the process) won't be recognized.

    Process proc = new Process();
    proc.StartInfo.FileName = "CMD.exe";
    proc.StartInfo.Arguments = "/c apktool d app.apk";
    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    proc.Start();
    proc.WaitForExit();
    
    0 讨论(0)
提交回复
热议问题