5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

.NET CUIを使ってVisual Studioでの面倒な作業から開放される。

Posted at

はじめに

C#やVBでの開発では基本的にVisual Studioで開発を進めることが多いと思います。
ただし、GUIツールなので面倒な画面遷移を強いられる事もおおく、コマンドでやってしまったほうが早い場合もあります。

ここでは、私が実務でも使っているコマンドをいくつか紹介したいと思います。

まずは入っているかを確認

.NET SDKが入っていれば大丈夫なはずです。

$  dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.300
 Commit:    8473146e7d

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19044
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.300\

global.json file:
  Not found

Host:
  Version:      6.0.10
  Architecture: x64
  Commit:       5a400c212a

.NET SDKs installed:
  3.1.413 [C:\Program Files\dotnet\sdk]
  5.0.400 [C:\Program Files\dotnet\sdk]
  5.0.401 [C:\Program Files\dotnet\sdk]
  6.0.300 [C:\Program Files\dotnet\sdk]

// 長いので省略します。

ソリューション・プロジェクトの管理

Visual Studioでソリューションを作成する場合、下記画像のようなウィザードで作成をすることになります。
(画像はVisual Studio 2022 Enterprise)
image.png

読み込みに時間がかかるし検索とかも面倒。というので、ここはコマンド叩いちゃいましょう。

ソリューションの作成

空のソリューションファイルを作成する場合はdotnet newを使います。
テンプレートをカレントディレクトリに展開するコマンドで、ソリューションテンプレートはslnです。
他にもいろんなテンプレートがあります。

dotnet new sln
$ cd .\DotnetCli\
$ dotnet new sln
The template "Solution File" was created successfully.

$ ls

    Directory: C:\dev\DotnetCli

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2022-11-24    01:10            442 DotnetCli.sln

.gitignoreファイルの作成

.gitignoreファイルは.NET向けの標準テンプレートがありますが、これも一発で作成してくれます。
.NETが生成するファイル郡はもちろん、Visual Studio / Visual Studio Code / その他JetBrains製品が生成するファイルも抑えているのでまず間違いないです。

dotnet new gitignore

最新版はここ。
https://github.com/github/gitignore/blob/main/VisualStudio.gitignore

ソリューションファイルの管理

ソリューションは空のままでは意味がないので、プロジェクトファイルを追加して使います。
dotnet slnを使います。

プロジェクトの追加

dotnet sln add MyClassLib.csproj

ソリューションフォルダに含める場合

dotnet sln add MyClassLib.csproj -s Commons

globパターンを使って一括で追加

dotnet sln add (ls -r **/*.csproj)

プロジェクトの削除

プロジェクトそのものは削除されません。

dotnet sln remove MyClassLib.csproj

まとめ

新規でものを作り出すときに、Visual Studioでの手続きは結構面倒なのですが、コマンドでパシパシできると楽ちんです。
新規構築以外でも、私は実務ではDeveloper Experienceの部分をいじっている事も多いのでこの辺のコマンドには助けられています。

ここで紹介したのは一部なので、興味があれば公式ドキュメントを御覧ください。

5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?