5

I'm developing .net core console application. I want to alert to user when want to exit application. Like below;

 MessageBox.Show("Contiue or not", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.No)
    Application.Exit();

But I can't add System.Windows.Forms referance to the my project. I'm getting this error.

 Error  CS0234  The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)    

Is that possible to show Message Box on .net core Console Application?

Mert DEMIRKIRAN
  • 418
  • 5
  • 18
  • https://stackoverflow.com/questions/1119841/net-console-application-exit-event maybe this helps – Muammer Jan 24 '20 at 14:52
  • 2
    Per the [porting instructions](https://learn.microsoft.com/dotnet/core/porting/winforms), changing the SDK to `Microsoft.NET.Sdk.WindowsDesktop` and adding `true` will make this work. (The output type has to be left to `Exe` if you want a console application.) Note that it remains inherently a bad idea to do GUI calls from a console app, of course, even ignoring the gratuitous added platform incompatibility (this only works on Windows). – Jeroen Mostert Jan 24 '20 at 15:05

2 Answers2

4

In order to use Windows Forms you need to modify .csproj:

  • set UseWindowsForms to true
  • append -windows to TargetFramework (e.g. net6.0-windows)

ConsoleApp.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
</Project>

Program.cs:

System.Console.WriteLine("Hello, Console!");
System.Windows.Forms.MessageBox.Show("Hello, Popup!");

Result:

Result

Notes:

  • I checked this solution on .NET Core 3.1, .NET 5 and .NET 6 on Windows x64.
  • If you don't want console window to be shown, set OutputType in .csproj to WinExe instead of Exe.
  • Windows Forms works only on Windows, so as this solution.
  • On .NET Core 3.1 there is no requirement to change target framework to Windows, however it is still impossible to publish executable for Linux OS.
  • If you need cross-platform solution that shows popups on Windows and only use console on Linux – you can create custom build configuration and use preprocessor directives as in the example below.

Cross-platform ConsoleApp.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <Configurations>Debug;Release;WindowsDebug;WindowsRelease</Configurations>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)' == 'WindowsDebug' Or '$(Configuration)' == 'WindowsRelease'">
    <DefineConstants>WindowsRuntime</DefineConstants>
    <TargetFramework>net6.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
</Project>

Cross-platform Program.cs

System.Console.WriteLine("Hello, Console!");

#if WindowsRuntime
System.Windows.Forms.MessageBox.Show("Hello, Popup!");
#endif
Daniil Palii
  • 975
  • 9
  • 21
  • 1
    Can this application be run on Linux (without the Windows dialog then)? – Heinrich Ulbricht May 09 '22 at 08:02
  • @HeinrichUlbricht, no, when project uses Windows Forms it can only target Windows. However, you can create custom build configuration and use preprocessor directives. I added a code example to the answer. – Daniil Palii May 10 '22 at 12:59
1

Some console apps need this line added to the project as well...

<DisableWinExeOutputInference>true</DisableWinExeOutputInference>

Adding this to the example in Daniil's post would give you this...

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>

    <!--Insert DisableWinExeOutputInference here -->
    <DisableWinExeOutputInference>true</DisableWinExeOutputInference>

  </PropertyGroup>
</Project>
Erik Toft
  • 31
  • 4