title: Show message in current cmd by c#
body: <p>I have written the application which works in console as well as a user interface. Now when executing the application from a console, I want to show the message in the currently opened console.</p>
  
  <pre><code>static class Program
      {
          [DllImport("kernel32.dll",
              EntryPoint = "GetStdHandle",
              SetLastError = true,
              CharSet = CharSet.Auto,
              CallingConvention = CallingConvention.StdCall)]
          private static extern IntPtr GetStdHandle(int nStdHandle);
          [DllImport("kernel32.dll",
              EntryPoint = "AllocConsole",
              SetLastError = true,
              CharSet = CharSet.Auto,
              CallingConvention = CallingConvention.StdCall)]
          private static extern int AllocConsole();
          private const int STD_OUTPUT_HANDLE = -11;
          private const int MY_CODE_PAGE = 437; 
  
          [STAThread]
          static void Main(string[] Args)
          {
                  if (Args[0] != "")
                  {
                      //AllocConsole();
                      commandlineTool(Args[0]);
                  }
                  else
                  {
  
                      Application.EnableVisualStyles();
                      Application.SetCompatibleTextRenderingDefault(false);
                      Application.Run(new Form2());
                  }
  static void commandlineTool(string filename)
  {
      //all coding here
      AllocConsole();
  
      IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
      SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
      FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
      System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
      StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
      standardOutput.AutoFlush = true;
      Console.SetOut(standardOutput);
  
      Console.WriteLine("File has been generated.");
      Console.ReadLine();
  }
  </code></pre>
  
  <p>Actually  <strong>AllocConsole()</strong> allocate new console for the process.</p>
  
  <p>But from this a new console opens and shows the message but I want to show the message in the same console.</p>