如何使用隐藏窗口启动进程

时间:2014-03-25 12:56:57

标签: c#

我有这段代码:

string d = "-f image2 -framerate 9 -i E:\\REC\\Temp\\%06d.jpeg -r 30 E:\\REC\\Video\\" + label1.Text + ".avi";

//string d = "-f dshow -i video=\"screen-capture-recorder\" E:\\REC\\" + label1.Text + ".flv";
Process proc = new Process();
proc.StartInfo.FileName = "E:\\ffmpeg\\bin\\ffmpeg.exe";
proc.StartInfo.Arguments = d;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;

if (!proc.Start())
{
    Console.WriteLine("Error starting");
    return;
}
proc.WaitForExit();

运行时,ffmpeg.exe就是这样:

enter image description here

我的问题是如何隐藏此窗口?

2 个答案:

答案 0 :(得分:3)

您需要以下设置组合:

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;

就是这样。

原因是密钥设置为CreateNoWindow,必须为true。但CreateNoWindow只有UseShellExecute false时才有效。这是因为CreateNoWindow映射到传递给CREATE_NO_WINDOW的{​​{1}}进程创建标志。仅CreateProcessCreateProcess时才会调用UseShellExecute

可以从documentation找到更多信息:

  

物业价值

     

true 如果应该启动进程而不创建包含它的新窗口;否则, false 。默认值为 false

     

<强>说明

     

如果UseShellExecute属性为true或UserName和Password   属性不为null,将忽略CreateNoWindow属性值   并创建一个新窗口。

答案 1 :(得分:1)

这将使所有进程保持在同一控制台窗口中。不允许开一个新的

Process process = new Process();

 // Stop the process from opening a new window
 process.StartInfo.RedirectStandardOutput = true;
 process.StartInfo.UseShellExecute = false;
 process.StartInfo.CreateNoWindow = true;

// Setup executable and parameters
  process.StartInfo.FileName = @"E:\\ffmpeg\\bin\\ffmpeg.exe"

 //Optional
  string d = "-f image2 -framerate 9 -i E:\\REC\\Temp\\%06d.jpeg -r 30 E:\\REC\\Video\\" + label1.Text + ".avi";
 process.StartInfo.Arguments = d;

 // Go
  process.Start();