1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#アプリのビルドと配布をPowerShellで自動化する

1
Last updated at Posted at 2026-06-16

想定

C#で作成したWindowsアプリを、社内LAN上の共有フォルダへ配置し、
利用者に動作確認してもらうケースを想定します。

手順

プロジェクトのルートフォルダで、以下のコマンドを実行します。

dotnet publish -c Release -r win-x64 --self-contained true

例えば、

C:\dev\ソリューション名

で作業していた場合は、C:\dev\ソリューション名 のカレントディレクトリで実行します。

実行後、次のフォルダが作成されます。

C:\dev\ソリューション名\プロジェクト名\bin\Release\net8.0-windows\win-x64\publish

配布時は、この publish フォルダを丸ごとコピーして渡します。

--self-contained true を指定しているため、配布先PCに .NET Runtime がインストールされていなくても実行できます。

配布用PowerShell

毎回手動でコピーするのが面倒な場合は、
PowerShellで配布作業まで自動化できます。

$SourcePath = "C:\dev\ソリューション名\プロジェクト名\bin\Release\net8.0-windows\win-x64\publish"
$TargetPath = "M:\個人名\データ更新アプリ"
$SpecFile   = "C:\dev\ソリューション名\データ更新アプリ仕様書.md"

dotnet publish -c Release -r win-x64 --self-contained true
if ($LASTEXITCODE -ne 0) {throw "dotnet publish に失敗しました。"}
Write-Host "コンパイル完了"

Get-ChildItem -Path $TargetPath -Force | Remove-Item -Recurse -Force
Write-Host "旧ファイル削除完了"
Copy-Item -Path (Join-Path $SourcePath "*") -Destination $TargetPath -Recurse -Force
Write-Host "新ファイル送信完了"
Copy-Item $SpecFile $TargetPath -Force
Write-Host "仕様書送信完了"

パス名は環境に合わせて読み替えてください。

PowerShellのコマンドは、学習用として敢えてシンプルにしています。

配布先フォルダの内容は全削除してからコピーしています。実行前に対象パスを十分確認してください。

まとめ

dotnet publish --self-contained true を使うことで、
配布先に .NET Runtime がなくても実行できるアプリを作成できます。

さらに PowerShell を組み合わせることで、
社内共有フォルダへの配布作業も自動化できます。

検証用ツールや社内向け小規模アプリを配布する際に便利でした。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?