Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Import Windows Forms to console application in C#

I have a console application and I just want to display a MessageBox at some point.

I found a page that stated that I could do it by adding a reference to the assembly. However, just adding using System.Windows.Forms doesn’t work (CS02348: it doesn’t exist in the namespace and I’m probably missing an assembly reference).

However, I can only seem to add COM references to my project. When I looked for a way to display the assembly panel, I found this page that seems to state that I should already have it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

According to this tutorial, I should manually browse and seek in C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App.

I tried with C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\7.0.2\System.Windows.Forms.dll and C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.13\System.Windows.Forms.dll. With both of them, I get the error CS1705:

Error CS1705 Assembly ‘System.Windows.Forms’ with identity
‘System.Windows.Forms, Version=7.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089’ uses ‘System.Runtime,
Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’
which has a higher version than referenced assembly ‘System.Runtime’
with identity ‘System.Runtime, Version=6.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a’

I just deleted the System.Windows.Forms.dll I manually imported and my *.csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="RandomLibrary">
      <HintPath>RandomLibrary.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

I need it to stay in 6.0 for the LTS.

Why don’t I have this assembly tab?
How to add reference to System.Windows.Forms? Why did I get the same message with version 6.0.13 than with version 7.0.2?

>Solution :

You don’t have to add anything in your Dependencies or manually reference any dlls. You just have to add a few lines to your csproj file.

Here is a fresh one generated by the console app wizard from Visual Studio:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

If you want to use win forms, modify it thus (right click on the project, select ‘Unload project’ or just edit it manually in a text editor):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWindowsForms>true</UseWindowsForms>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Note the addition of -windows after net6.0 as well as <UseWindowsForms>true</UseWindowsForms>.

Then you can use win forms and see console output:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
string message = "Hello,";
string caption = "World!";

MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;

// Displays the MessageBox.
result = MessageBox.Show(message, caption, buttons);

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading