11

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?

Gray
  • 7,050
  • 2
  • 29
  • 52
orglce
  • 513
  • 2
  • 7
  • 19

5 Answers5

11

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();
Daniel Abou Chleih
  • 2,440
  • 2
  • 19
  • 31
6

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
Gray
  • 7,050
  • 2
  • 29
  • 52
2

Try this :

     proc.StartInfo.CreateNoWindow = true;
     proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
     proc.WaitForExit(); 
sino
  • 754
  • 1
  • 7
  • 22
0

Try this:

Process myProcess = new Process();
myProcess.StartInfo.CreateNoWindow = true;

Also, read the documentation.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
-1
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;
Gray
  • 7,050
  • 2
  • 29
  • 52
Dimo
  • 3,238
  • 6
  • 29
  • 46
  • 4
    Adding the extra stuff, from wherever that is copied from, makes this answer kind of confusing, especially with no explanation of what the code is doing. – Gray Oct 08 '13 at 20:05
  • 3
    A little explanation in your post goes a long way. – gunr2171 Oct 08 '13 at 20:07
  • 1
    @Dimo I can't force you to do it, I was just offering some advice for improving your answer. That's what the site is about - the community working together to solve problems like this. It isn't about spoon-feeding, it is about providing well-written, helpful, clear, and complete answers. – Gray Oct 08 '13 at 20:08
  • 1
    Some [relevant](http://meta.stackexchange.com/questions/95470/down-vote-code-only-answers) meta discussion. Keep in mind that we don't just write answers for OP's context, but also future visitors (with varying levels of competency) with the same question. – Anthony Neace Oct 08 '13 at 20:13