Manu
Manu

Reputation: 4137

Executing an external program via c# without showing the console

I am trying to run VLC from my C# Console Application, but I cannot. I know there are other similar questions (e.g. Launching process in C# Without Distracting Console Window and C# Run external console application and no ouptut? and C#: Run external console program as hidden) and from them I derived the following code:

        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();

However, the console still shows up, both when I comment and uncomment the WindowStyle line. What's wrong?

Upvotes: 3

Views: 6291

Answers (3)

Agat
Agat

Reputation: 4779

As it says here, just do the following:

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
     }

updated:

You also should add WaitForInputIdle after the process starting:

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

Upvotes: 1

Wagner DosAnjos
Wagner DosAnjos

Reputation: 6374

Try the following command line switch. It's documented here.

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

Upvotes: 2

Nathan Abramov
Nathan Abramov

Reputation: 115

You could simply change the Output type in the project properties to Windows Application. Just go right click on the project > properties > Application

Upvotes: 0

Related Questions