Reputation: 398
I have a single dotnet-core GUI application that depending on the argument parameters it may show only the GUI interface (done with Avalonia framework) or if it receives some special arguments then it should work in console-mode and log into the console without showing the GUI.
If I use <OutputType>WinExe</OutputType>
, then the GUI shows fine but the app will never log into the console (for example when using Console.WriteLine("some-text")
).
This means I need to configure the csproj with <OutputType>Exe</OutputType>
in order to be able to log into the console anything, the problem is that when running in GUI mode, along with the GUI application window, another console window pops up which I want to hide to show only the GUI.
In dotnet full-framework 4.X I was able to create WPF apps that did this dual console/gui mode in 1 exe. A possible solution would be if I could make the console window to be hidden when running in GUI mode, or maybe Avalonia-UI/dotnet-core has some other way to do this. Does anyone knows how to this?
UPDATE: Here are some clarifications: The app has 2 modes:
<OutputType>WinExe</OutputType>
, but that makes the Console.Write
function not to work when run in the following Console Mode
.app.exe -h
to show the available commands, app.exe -v
to show the version, etc. This means that it is not OK to show some hand-made GUI window displaying the output text or log anywhere else like into a file.It could be done in 2 separate executable files, but some people like me find some beauty in applications that are just a simple app.exe file.
There are many examples of this dual behavior, like VisualStudio's Nirsoft applications.devenv.exe
or
Upvotes: 5
Views: 2476
Reputation: 3286
If I get you right you want to start the console on special parameter and WinForms application on the other hand.
First you have to set
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
In your project file. See documentation
Try to set the <OutputType>
back to Exe
and add this to your Program > Main:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
IntPtr h = GetConsoleWindow();
// switch here to show or not show console window
ShowWindow(h, 0);
// ShowWindow(h, 1);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Hide your WinForms application manually if parameter is set
var startup = new MainFormStartup();
Application.Run(startup.CreateForm<MainWindow>());
}
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
}
I tried this locally with
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
</PropertyGroup>
</Project>
But to be honest in my optinion you should have two exe files compiled with a shared library to accomplish a console exe file and a UI-exe file, fe.:
yourcode.csproj (Your Businesslogic)
Upvotes: 3