question

ThomasGssi-7359 avatar image
0 Votes"
ThomasGssi-7359 asked ·

Show/Hide Console or HowTo Write to Console in a Windows Application in .NET 5.0

Hi There

I plan to make a mixed application, means, the user can start them to open a black console window or to open it with a WPF-GUI. The user can select with a start option like -console or -window.

Thus I created a WPF-application and set the options in the .csproj to:

   <PropertyGroup>
     <TargetFramework>net5.0-windows</TargetFramework>
     <UserSecretsId>dotnet-WorkerService2-xxx...</UserSecretsId>
     <UseWPF>true</UseWPF>
     <OutputType>Exe</OutputType>
     <DisableWinExeOutputInference>True</DisableWinExeOutputInference>
   </PropertyGroup>

Dependion on the start option, I create a new thread with a WPF-application, that opens the WPF-GUI.
When I use the GUI, I hide the opening console window with:

 using System.Runtime.InteropServices;
 [DllImport("kernel32.dll")]
 static extern IntPtr GetConsoleWindow();
 [DllImport("user32.dll")]
 static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

When I use the console, I do not hide them and do not create the WPF-application inside.
So then I can also see, that logs are put into the console as well as I can write text into the console window.

So it works fine.

But the problem is, when I start the application as a WPF-GUI, the console window pops up shortly and then disappears, as soon as the code has been executed to hide them, after the application has been started.
This is a little bit ugly !

Thus, I tried to change the application to run them as a windows application. Starting it, no console is created or opened. So I create them inside the application with the following code, when I need it:

 using System.Runtime.InteropServices;
 [DllImport("kernel32.dll", SetLastError = true)]
 internal static extern int AllocConsole();

and then free them at the end:

 [DllImport("kernel32.dll", SetLastError = true)]
 internal static extern int FreeConsole();

And if the application is started within a console, I tried to get these console using:

 using System.Runtime.InteropServices;
 [DllImport("kernel32.dll")]
 static extern IntPtr GetConsoleWindow();

But the problem is, neither the log nor messages, that I want to write to the console, are shown in the console. Although I create one or get the one, that is opened from where I started the program, I cannot us them.

So I tried two ways to reach my goal, but none of them works satisfactory.
Do you have an idea, how I can solve this problem in one of the following ways:

  1. Run the application as a console application, but prevent, that the console pops up shortly after starting, if I don't use them. Maybe a console application can be started with a hidden console window, so that I can use the upper commands in kernel32.dll, the show them when I need. Thus logs and messages would be shown in the console.

  2. Run the application as a windows application. But how can I reach the already existing console, where I did start the program or allocate a new one, if the application was started within explorer, so that logs and messages are written to them.



dotnet-csharpwindows-wpfdotnet-runtime-coredotnet-console
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Will-9174 avatar image
0 Votes"
Will-9174 answered ·

@Thomas,

You could use Console.Write() or Console.readline() to use your console windows.

 private void Button_Click(object sender, RoutedEventArgs e)
         {
             AllocConsole();
             Console.WriteLine("Console text");
             InteractiveConsole();
         }
    
         private void InteractiveConsole() 
         {
             Console.WriteLine("Console Interactive");
             string firststr = Console.ReadLine();
             Console.WriteLine("output states {0}", firststr);
    
    
         }
    
         private void Button_Click_1(object sender, RoutedEventArgs e)
         {
             //Close Console
             Console.Write("close concole");
             FreeConsole();
         }


If you want to run WPF application as console application, you may need custom WPF control like this:
https://stackoverflow.com/questions/14948171/how-to-emulate-a-console-in-wpf
If use InteropServices to apply console windows, you may need code to receive input message and handle it
BR,






·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.