简体   繁体   中英

How to run dotnet Core console application that shows the console window or not based on argument parameters?

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.XI 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:

  • GUI Mode : if you just double click on the "App.exe" (runs with no args parameters) then the GUI should appear (and only the GUI, not the GUI plus another console window as it currently happens). This would be usually done by setting the csproj as <OutputType>WinExe</OutputType> , but that makes the Console.Write function not to work when run in the following Console Mode .
  • Console Mode : if an advanced user is trying to use some of the app functions from the console automate something, then the app should behave like a console application, with all the features one would expect like 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 devenv.exe or Nirsoft applications.

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)

  • yourapp-console.csproj (Console Application: with a reference to yourcode.csproj)
  • yourapp-ui.csproj (WinForms Application: with a reference to yourcode.csproj)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM