通过c#执行外部程序而不显示控制台

时间:2013-11-03 19:10:14

标签: c# console-application

我正在尝试从我的C#控制台应用程序运行VLC,但我不能。我知道还有其他类似的问题(例如Launching process in C# Without Distracting Console WindowC# Run external console application and no ouptut?以及C#: Run external console program as hidden),我从中得出以下代码:

        Process process = new Process();
        process.StartInfo.FileName = "C:\\Users\\XXXXX\\Desktop\\VLC\\vlc.exe";
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        //process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.StartInfo.Arguments = " -I dummy";

        process.Start();

但是,当我评论和取消注释WindowStyle行时,控制台仍会显示。 怎么了?

3 个答案:

答案 0 :(得分:2)

尝试以下命令行开关。它记录在案here

process.StartInfo.Arguments = "-I dummy --dummy-quiet";

答案 1 :(得分:1)

正如here所述,只需执行以下操作:

using System.Runtime.InteropServices;

...
  [DllImport("user32.dll")]
  public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

  [DllImport("user32.dll")]
  static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

...

     //Sometimes System.Windows.Forms.Application.ExecutablePath works for the caption depending on the system you are running under.
     IntPtr hWnd = FindWindow(null, "Your console windows caption"); //put your console window caption here
     if(hWnd != IntPtr.Zero)
     {
        //Hide the window
        ShowWindow(hWnd, 0); // 0 = SW_HIDE
     }


     if(hWnd != IntPtr.Zero)
     {
        //Show window again
        ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
     }

更新

您还应该在流程开始后添加WaitForInputIdle:

process.Start();
process.WaitForInputIdle();

答案 2 :(得分:0)

您只需将项目属性中的输出类型更改为Windows应用程序即可。 只需右键单击项目>属性>申请