Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run CMD command without displaying it?

Tags:

c#

I have created a Process to run command in CMD.

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

How can I run this command without displaying actual CMD window?

like image 364
orglce Avatar asked Oct 08 '13 19:10

orglce


People also ask

How do I get a blank Command Prompt?

Open the File menu, select "Run new task," and type cmd in the "Create new task" window. Hit Enter on your keyboard or press the OK button, and the Command Prompt opens.

How do I run a Command Prompt in the background?

If you want to run additional commands while a previous command runs, you can run a command in the background. If you know you want to run a command in the background, type an ampersand (&) after the command as shown in the following example. The number that follows is the process id.


3 Answers

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();
like image 72
Daniel Abou Chleih Avatar answered Nov 02 '22 13:11

Daniel Abou Chleih


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
like image 20
Gray Avatar answered Nov 02 '22 13:11

Gray


Try this :

     proc.StartInfo.CreateNoWindow = true;
     proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
     proc.WaitForExit(); 
like image 20
sino Avatar answered Nov 02 '22 14:11

sino