C#のConsoleアプリケーション開発でSystem.Windows.Formsを使う

はじめに

Visual Studio Code で コンソールプログラムを作成するときですが フォームアプリケーションを操作したいなと思ったときにusingが足りなくて困ったのでそのTipsです。

動作環境

  • Windows10 Pro 21H2
  • Visual Studio Code
  • dotNET Core 6.0.101

結論

.csproj ファイルに追記するだけでOKです。

方法

これがデフォルトの.csproj ファイルの中身です。

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

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

</Project>

上記の記載を以下のように変更します。

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

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

</Project>

これでConsoleアプリケーション から Sysmtem.Windows.Forms を利用できます。

おわり