設定内容をファイルに保存する (ApplicationSettingsBase利用) - C#

アプリケーションの設定内容を保存したい場合があります。アプリケーションの設定内容を保存する方法としてはiniファイルやレジストリ、XMLファイルに書き込むなどいくつかの保存方法があります。今回は手軽にアプリケーション設定保存が扱えるApplicationSettingsBaseクラスを利用します。

下記のコードは設定内容を設定ファイル(user.config)に保存する例です。

main.cs (ButtonClickイベント)

private void button1_Click(object sender, EventArgs e)
{
  AppSettings apps = new AppSettings();
  //設定クラスに値を設定して、保存
  apps.data = "ABCD";
  apps.Save();
}

AppSettings.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace App
{
  class AppSettings : ApplicationSettingsBase
  {
    [UserScopedSetting]
    public string data
    {
      get
      {
        return (string)this["data01"];
      }
      set
      {
        this["data01"] = value;
      }
    }
  }
}
まず、ApplicationSettingsBaseを継承したクラスを用意します。今回はAppSettingsクラスとしました。設定を格納するフィールドをプロパティとして実装します。今回は文字列型のdataプロパティ一つを用意します。プロパティのsetとgetを実装します。データの格納先はthis["(任意の識別名)"]にします。今回はdata01としました。
AppSettingsクラスを利用する場合は、まず、AppSettings apps = new AppSettings();でクラスのインスタンスを作成し、インスタンスのプロパティに保存する設定内容を代入します。上記の例では、dataプロパティに"ABCD"を代入しています。代入が済んだのち。apps.Save();で保存内容をファイルに書き込みます。

設定ファイル(user.config)は、以下のコードで取得できるファイルパスの位置に保存されます。
Configuration conf = ConfigurationManager.OpenExeConfiguration(
  ConfigurationUserLevel.PerUserRoamingAndLocal);
textBox_ConfigFilePath.Text = conf.FilePath;

やや複雑な例

main.cs (ButtonClickイベント)

private void button1_Click(object sender, EventArgs e)
{
  //設定クラスに値を設定して、保存
  apps.Layout = "ABCD";
  apps.WindowLayout = AppSettings.WindowLayoutType.WL_BOTTOM;
  apps.Port = 110;
  apps.Save();
}

AppSettings.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace App
{
  class AppSettings : ApplicationSettingsBase
  {
    public enum WindowLayoutType { WL_TOP, WL_BOTTOM };

    [UserScopedSetting]
    public WindowLayoutType WindowLayout
    {
      get
      {
        return (WindowLayoutType)this["WindowLayout"];
      }
      set{
        this["WindowLayout"] = value;
      }
    }

    [UserScopedSetting]
    public string CaptionText
    {
      get
      {
        return (string)this["CaptionText"];
      }
      set
      {
        this["CaptionText"] = value;
      }
    }

    [UserScopedSetting]
    public int Port
    {
      get
      {
        return (int)this["Port"];
      }
      set
      {
        this["Port"] = value;
      }
    }
  }
}


下記のコードのように実装すれば、配列を設定保存することもできます。下記コードではクラスの配列を保存しています。

main.cs (ButtonClickイベント)

private void button1_Click(object sender, EventArgs e)
{
  Account[] ac = new Account[2];
  ac[0] = new Account();
  ac[0].Name = "1";
  ac[0].Port = 1;
  ac[1] = new Account();
  ac[1].Name = "2";
  ac[1].Port = 2;
  apps.AccountArray = ac;
      
  //設定クラスに値を設定して、保存
  apps.Layout = "ABCD";
  apps.WindowLayout = AppSettings.WindowLayoutType.WL_BOTTOM;
  apps.Port = 110;
  apps.Save();
}

AppSettings.cs

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace App
{
  public class Account
  {
    public string Name;
    public int Port;
  }


  class AppSettings : ApplicationSettingsBase
  {
    public enum WindowLayoutType { WL_TOP, WL_BOTTOM };

    [UserScopedSetting]
    public Account[] AccountArray
    {
      get
      {
        return (Account[])this["AccountArray"];
      }
      set
      {
        this["AccountArray"] = value;
      }
    }

    [UserScopedSetting]
    public WindowLayoutType WindowLayout
    {
      get
      {
        return (WindowLayoutType)this["WindowLayout"];
      }
      set{
        this["WindowLayout"] = value;
      }
    }

    [UserScopedSetting]
    public string Layout
    {
      get
      {
        return (string)this["Layout"];
      }
      set
      {
        this["Layout"] = value;
      }
    }

    [UserScopedSetting]
    public int Port
    {
      get
      {
        return (int)this["Port"];
      }
      set
      {
        this["Port"] = value;
      }
    }
  }
}
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-01-06
作成日: 2010-06-03
iPentec all rights reserverd.