title: How to get process id of Console in C# console app
body: <p>In my hello world console app, <code>Process.GetCurrentProcess().Id</code> property returns a different value from the <code>Id</code> for the <code>Console</code> window created to display the app's stdout etc.</p>
  
  <p>How do I get the process id for the console window specifically?</p>
  
  <p>I cycle through the processes in <code>Process.GetProcesses()</code> and check for the console window based on window title. When it finds it, I print it's process id out and it's different to what is returned from the <code>GetCurrentProcess()</code> call. So I'm concluding the console app process and the console window are two different processes, maybe the console window is a child process of my console app, or maybe it's a peculiarity associated with running a console app from within Visual Studio.</p>
  
  <pre><code>        Process[] processlist = Process.GetProcesses();
          int origProcessId = Process.GetCurrentProcess().Id;
          foreach ( Process p in processlist)
          {
              // get all window handles of title 'C:\Windows\system32\cmd.exe
              if (!String.IsNullOrEmpty(p.MainWindowTitle) &amp;&amp; p.MainWindowTitle.IndexOf("C:\\Windows\\system32\\cmd.exe") == 0 )
              {
                      Console.WriteLine("Gets here ok, once &amp; only once");
                      if(origProcessId == p.Id){
                                  Console.WriteLine("Process: {0}", p.Id); // doesn't get here!!!
                      }
              }
          }
  </code></pre>