bucket-sort logo bucket-sort

プログラミングとインフラエンジニアリングの覚え書き

  • Posts
  • About
  • Contact
  1. Home
  2. All Posts
  3. [C#] .NETが用意しているアプリケーションの設定クラスを試す - ConfigurationBuilder

[C#] .NETが用意しているアプリケーションの設定クラスを試す - ConfigurationBuilder

Jan 20, 2026 C# , .NET bucket-sort

Microsoft.Extensions.Configuration.ConfigurationBuilder は、

アプリケーション設定を「さまざまなソース」から読み込み、1つの設定オブジェクトとして統合するための仕組み

です。もともとは ASP.NET Core 用に設計されましたが、現在はコンソールアプリや Worker Service、WinForms / WPF でも普通に使われています。

ConfigurationBuilder の概要

ConfigurationBuilder は

  • JSON
  • 環境変数
  • コマンドライン引数
  • INI ファイル
  • メモリ上の値
  • Azure App Configuration など

といった 複数の設定ソースを合成するためのビルダーです。

最終的に IConfiguration を生成します。

ConfigurationBuilder の位置づけ (というか役回り)

ConfigurationBuilder は

  • 設定を「保存するクラス」ではない
  • 設定を「読み込んで統合するクラス」

です。つまり

  • JSONシリアライズ用クラスではありません。

保存処理(Write)は標準では提供されません。 読み取り専用に近い設計です。

テストコード

// 設定ファイルの使用例
Console.WriteLine("\n=== Microsoft.Extensions.Configuration の使用例 ===");

// ConfigurationBuilderを使ってappsettings.jsonから設定を読み込む
var configuration = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .Build();

// 方法1: 個別の値を取得
Console.WriteLine("\n[方法1: 個別の値を取得]");
var theme = configuration["AppSettings:Theme"];
var fontSize = configuration["AppSettings:FontSize"];
Console.WriteLine($"Theme: {theme}");
Console.WriteLine($"FontSize: {fontSize}");

// 方法2: セクションを取得
Console.WriteLine("\n[方法2: セクションを取得]");
var appSettingsSection = configuration.GetSection("AppSettings");
Console.WriteLine($"Theme: {appSettingsSection["Theme"]}");
Console.WriteLine($"FontSize: {appSettingsSection["FontSize"]}");
Console.WriteLine($"EnableNotifications: {appSettingsSection["EnableNotifications"]}");

// 方法3: 強く型付けされたオブジェクトにバインド(推奨)
Console.WriteLine("\n[方法3: 強く型付けされたオブジェクトにバインド]");
var settings = appSettingsSection.Get<AppSettingsStore.SettingsDto>();
if (settings != null)
{
    Console.WriteLine($"Theme: {settings.Theme}");
    Console.WriteLine($"FontSize: {settings.FontSize}");
    Console.WriteLine($"EnableNotifications: {settings.EnableNotifications}");
}

// 注意: Microsoft.Extensions.Configurationは読み込み専用
// 設定を保存するには、引き続きJsonSerializerを使う
Console.WriteLine("\n[設定の保存には JsonSerializer を使用]");
var appSettingsStore = new AppSettingsStore("SampleJson");
var currentSettings = appSettingsStore.Load();
Console.WriteLine($"Current saved settings - Theme: {currentSettings.Theme}, FontSize: {currentSettings.FontSize}");

参考サイト

ConfigurationBuilder Class (Microsoft.Extensions.Configuration) | Microsoft Learn
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.configurationbuilder?view=net-10.0-pp

C# .NET Windows Code Snippet 設定 ConfigurationBuilder
← [C#] アプリケーションの設定をJSON形式で保存する - JsonSerializer AlmaLinux 9.5のPHPをアップデートする (8.2 → 8.3 → 8.4) →

Related Posts

  • [C#] アプリケーションの設定をJSON形式で保存する - JsonSerializer Jan 19, 2026
  • [C#] グローバルフックとキーボードフックを理解する - SetWindowsHookExの使い方 Jan 25, 2026
  • [C#] アプリケーションのデータをJSONにシリアライズする - JsonSerializer Jan 18, 2026
  • [C#] Windows のプロセスを理解する: Process / Thread / ProcessStartInfo 徹底入門 Jun 23, 2026

Table of Contents

  • ConfigurationBuilder の概要
  • ConfigurationBuilder の位置づけ (というか役回り)
  • テストコード
  • 参考サイト

Recent Posts

  • [WPF] XAML の構文を理解する: 名前空間とキーワード Aug 23, 2026
  • [WPF] Window クラスを支える基底クラス Aug 22, 2026
  • [WPF] Window クラスの役割と上位クラス Aug 21, 2026
  • [WPF] Application クラスの役割 Aug 20, 2026
  • [WPF] WPF の主要アセンブリと名前空間 Aug 19, 2026

Categories

  • C#165
  • .NET164
  • AWS27
  • Entity Framework Core24
  • Laravel16
  • Linux15
  • MySQL9
  • Apache8
  • PHP8
  • Data Access6
  • DynamoDB6
  • セキュリティ6
  • Nginx5
  • WordPress4
  • インフラ4
  • テスト4
  • Hugo3
  • .NET Framework1
  • Aurora1
  • Diagnostics1

Tags

  • C#
  • .NET
  • Entity Framework Core
  • AWS
  • Laravel
  • コレクション
  • PHP
  • セキュリティ
  • MySQL
  • Linux
  • パフォーマンス
  • LINQ
  • WPF
  • Apache
  • SQL Server
  • System.Collections.Generic
  • デリゲート
  • リフレクション
  • ADO.NET
  • Code Snippet
  • DbContext
  • DynamoDB
  • NoSQL
  • PHP-FPM
  • RDS
  • System.Collections
  • Windows
  • メタデータ
  • メモリ管理
  • CIL
  • DoS
  • Nginx
  • WordPress
  • アセンブリ
  • ラムダ式
  • 監視
  • 設計
  • Amazon Linux 2023
  • Delegate
  • Docker
  • IDisposable
  • Ipset
  • Iptables
  • LINQ to Objects
  • Migration
  • OPCache
  • Pointer
  • Reflection
  • System.Collections.Specialized
  • Unsafe
Powered by Hugo & Explore Theme.