运行CMD命令而不显示它
本文关键字:显示 CMD 命令 运行 | 更新日期: 2023-09-27 18:34:31
我创建了一个进程来运行CMD命令。
var process = Process.Start("CMD.exe", "/c apktool d app.apk");
process.WaitForExit();
如何在不显示实际CMD窗口的情况下运行此命令?
您可以使用
WindowsStyle-Property 来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
源:属性: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();
正如各种评论和答案中指出的那样,您的程序存在几个问题。我试图在这里解决所有这些问题。
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();
主要问题:
- 在不必要的情况下使用
cmd /c
- 在不设置隐藏应用的属性的情况下启动应用
试试这个:
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.WaitForExit();
试试这个:
Process myProcess = new Process();
myProcess.StartInfo.CreateNoWindow = true;
另外,请阅读文档。
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;