11

我在 CMD 中创建了一个运行命令的进程。

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

如何在不显示实际 CMD 窗口的情况下运行此命令?

4

4 回答 4

11

您可以使用 WindowsStyle-Propertyindicate 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

来源: 属性:MSDN 枚举:MSDN

并将您的代码更改为此,因为您在初始化对象时启动了该过程,因此将无法识别属性(在启动该过程后设置的属性)。

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();
于 2013-10-08T19:57:46.943 回答
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
于 2013-10-08T20:18:28.897 回答
2

尝试这个 :

     proc.StartInfo.CreateNoWindow = true;
     proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
     proc.WaitForExit(); 
于 2013-10-08T19:56:43.793 回答
-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;
于 2013-10-08T20:03:45.567 回答