Run CMD command without displaying it?

I created a process to run a command in CMD.

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

How to run this command without displaying the actual CMD window?

+6
source share
4 answers

There are several problems with your program, as indicated in various comments and answers. I tried to contact 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 style to 'hidden' psi.WindowStyle = ProcessWindowStyle.Hidden; var proc = new Process(); proc.StartInfo = psi; proc.Start(); proc.WaitForExit(); 

Main problems:

  • using cmd /c if not necessary
  • launching an application without setting properties to hide it
+5
source

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 because you started the process when you initialized the object, so the properties (which were set after the process started) will not 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(); 
+4
source

Try the following:

  proc.StartInfo.CreateNoWindow = true; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.WaitForExit(); 
+1
source
 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.FileName = "dcm2jpg.exe"; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.Arguments = "-fj -o \"" + ex1 + "\" -z 1.0 -sy " + ex2; 
-1
source

Source: https://habr.com/ru/post/955524/


All Articles