title: c# ShowWindow (hWnd, 0) not hiding window
body: <p>When I compile the below code into an executable and run it, the console windows appears.  From the texts that I've read <code>ShowWindow(hWnd,0)</code> should hide the console window, but it doesn't.</p>
  
  <p>This is the below code:</p>
  
  <pre><code>using System;
  using System.Runtime.InteropServices;
  using System.Text;
  using System.IO;
  using System.Threading;
  
  namespace Foreground {
    class GetForegroundWindowTest {
  
      /// Foreground dll's
      [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
      public static extern IntPtr GetForegroundWindow();
  
      [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
      public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  
      /// Console hide dll's
      [DllImport("kernel32.dll")]
      static extern IntPtr GetConsoleWindow();
  
      [DllImport("user32.dll")]
      static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  
      const int SW_HIDE = 0;
  
      public static void Main(string[] args){
          while (true){
              IntPtr fg = GetForegroundWindow(); //use fg for some purpose
  
              var bufferSize = 1000;
              var sb = new StringBuilder(bufferSize);
  
              GetWindowText(fg, sb, bufferSize);
  
              using (StreamWriter sw = File.AppendText("C:\\Office Viewer\\OV_Log.txt")) 
              {
                  sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss,") + sb.ToString());
              }
  
              var handle = GetConsoleWindow();
              Console.WriteLine(handle);
              ShowWindow(handle, SW_HIDE);
  
              Thread.Sleep(5000);
          }
      }
    }
  }
  </code></pre>
  
  <p>Can someone explain to me where the problem in the code is?</p>
  
  <p><code>Console.WriteLine(handle)</code> was a line put in to show me and you that the program is grabbing the handle, but it just isn't minimising the window to which the handle represents.</p>
  
  <p>Please note:  I'd refer a code based answer as opposed to an "alter IDE settings" answer.</p>