3

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?

Community
  • 1
  • 1
Manu
  • 4,019
  • 8
  • 50
  • 94
  • What's VLC? Is it possible that VLC is instantiating it's own console window? – Wagner DosAnjos Nov 03 '13 at 19:36
  • It's http://www.videolan.org/. Yes, it might be actually. Via -I dummy i avoid the UI to start. – Manu Nov 03 '13 at 19:44
  • I installed VLC and tried `-I dummy` from the command line. It does launch a 2nd window. It seems you may need to programmatically find that window and hide it. – Wagner DosAnjos Nov 03 '13 at 19:53
  • Uhm... agat suggested the use of FindWindow, but I cannot get it hidden: I think I cannot find it by name. Also, if I drop -I dummy, the normal UI starts; if I look for the name, no luck – Manu Nov 03 '13 at 20:04

3 Answers3

2

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

process.StartInfo.Arguments = "-I dummy --dummy-quiet";
Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29
1

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();
Agat
  • 4,577
  • 2
  • 34
  • 62
  • Still showing. I added the DllImport in the class definition and the following code within the Main method. No luck. I can only hide the launching application, not the launched one. – Manu Nov 03 '13 at 19:20
  • Yes. Right, you should look for the executed application window, not executing. – Agat Nov 03 '13 at 19:32
  • Yes, I tried that and I inserted C:\\Users\\XXXXX\\Desktop\\VLC\\vlc.exe as caption to look for, but it doesn't seem to work. I actually don't even know how to change the caption of the externally run process. – Manu Nov 03 '13 at 19:34
  • Hm... But the window caption won't be the path. You should find it somehow in another way. Try something like this: http://superuser.com/questions/378790/how-to-get-window-title-in-windows-from-shell. – Agat Nov 03 '13 at 19:42
  • It says the name is C:\Users\XXXXX\Desktop\VLC\vlc.exe – Manu Nov 03 '13 at 19:54
  • Ok. I've tried all the stuff... and now it even works for me. You should also add this line: process.WaitForInputIdle(); after process.Start(); – Agat Nov 03 '13 at 20:16
  • nope, still not doing right. However, the solution proposed in my accepted answer fixed the problem. Thank you for your advice on FindWindow, it was still useful! – Manu Nov 03 '13 at 20:29
0

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