Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 747
Manu Avatar asked Aug 06 '26 07:08

Manu


2 Answers

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

process.StartInfo.Arguments = "-I dummy --dummy-quiet";
like image 57
Wagner DosAnjos Avatar answered Aug 08 '26 21:08

Wagner DosAnjos


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();
like image 45
Agat Avatar answered Aug 08 '26 20:08

Agat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!