アプリケーション情報を記録する方法 (C#)

.NET Framework でアプリケーション情報を保存する方法について記載します。

 

 

 

.NET Framework でアプリケーション情報を保存する方法はいろいろあります。
代表的な下記方法について記載します。

 

方式の比較:
  方式 説明 保存される場所
1 ApplicationSettings Properties.Settings に保存(最も実務的)
Visual Studio が自動生成するユーザー設定
AppData\Local\<Company>\<App>\<Version>\user.config
2 Windows レジストリ Windows レジストリに保存 HKEY_CURRENT_USER\Software\<Company>\<App>\
3 JSON ファイル 独自 JSON ファイルに保存 任意の場所
推奨は以下の場所 (AppData\Roaming)
%APPDATA%\<Company>\<App>\settings.json
4 XML ファイル 独自 XML ファイルに保存 任意の場所
推奨は以下の場所 (AppData\Roaming)
%APPDATA%\<Company>\<App>\settings.xml
5 INI ファイル 独自 INI ファイルに保存 任意の場所
推奨は以下の場所 (AppData\Roaming)
%APPDATA%\<Company>\<App>\settings.ini

 

 

1. ApplicationSettings

.NET, .NET Framework アプリでは、特に理由なければ通常はこの方法でアプリ情報を保存するのが最も簡単で良いでしょう。

 

✔ 特徴

 

[評価環境]

コンパイラ : Visual Studio 2026, Version 18.10.0
OS : Windows11, 25H2

 

保存例(WinForms)

private void Form1_Load(object sender, EventArgs e)
{
    if (Properties.Settings.Default.Location != Point.Empty)
    {
        this.Location = Properties.Settings.Default.Location;
        this.Size = Properties.Settings.Default.Size;
    }
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Properties.Settings.Default.Location = this.Location;
    Properties.Settings.Default.Size = this.Size;
    Properties.Settings.Default.Save();
}

 

保存例(WPF)

using System.Windows;

namespace property_settings
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // アプリケーションの設定をアップグレードする
            if (!Properties.Settings.Default.HasUpgradedSettings)
            {
                // Upgrade メソッドを呼び出して、以前のバージョンの設定を新しいバージョンに移行します
                Properties.Settings.Default.Upgrade();

                // HasUpgradedSettings は User スコープの bool 設定として追加します。
                // これにより、アプリ更新後の初回起動時だけ旧バージョンの設定を引き継げます。
                Properties.Settings.Default.HasUpgradedSettings = true;

                // 設定を保存します
                Properties.Settings.Default.Save();
            }

            // ウィンドウの位置を設定する
            this.Left = Properties.Settings.Default.position_x;
            this.Top = Properties.Settings.Default.position_y;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // ウィンドウの位置を保存する
            Properties.Settings.Default.position_x = this.Left;
            Properties.Settings.Default.position_y = this.Top;
            Properties.Settings.Default.Save();
        }
    }
}

 

Visual Studio 設定例
Visual Studio 設定例

 

"AppData\Local\<Company>\<App>\<Version>\user.config" 例

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <property_settings.Properties.Settings>
            <setting name="position_x" serializeAs="String">
                <value>381.33333333333331</value>
            </setting>
            <setting name="position_y" serializeAs="String">
                <value>138</value>
            </setting>
            <setting name="HasUpgradedSettings" serializeAs="String">
                <value>True</value>
            </setting>
        </property_settings.Properties.Settings>
    </userSettings>
</configuration>

 

Save() は必要ですが Load() は不要(ありません)のようです。アプリケーション起動時に自動的に読み込みをやってくれているようです。

上記のようにとても簡単に機能を実現可能です。

 

 


 

2. Windows レジストリ

 

✔ 特徴

 

保存例 (c#)

using Microsoft.Win32;

RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\MyApp");
key.SetValue("Left", this.Left);
key.SetValue("Top", this.Top);
key.SetValue("Width", this.Width);
key.SetValue("Height", this.Height);

 

 


 

3. JSON ファイル

 

✔ 特徴

 

保存例 (c#)

var info = new WindowInfo
{
    Left = this.Left,
    Top = this.Top,
    Width = this.Width,
    Height = this.Height
};

string dir = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
    "MyCompany", "MyApp");

if (!Directory.Exists(dir)) {
    Directory.CreateDirectory(dir); 
}

string path = Path.Combine(dir, "settings.json");
File.WriteAllText(path, JsonConvert.SerializeObject(info));

 

 


 

4. XML ファイル

 

✔ 特徴

 

保存例 (c#)

var doc = new XDocument(
    new XElement("Window",
        new XElement("Left", this.Left),
        new XElement("Top", this.Top),
        new XElement("Width", this.Width),
        new XElement("Height", this.Height)
    )
);

string dir = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
    "MyCompany", "MyApp");

if (!Directory.Exists(dir)) {
    Directory.CreateDirectory(dir); 
}

string path = Path.Combine(dir, "setting.xml");
doc.Save(path);

 

 


 

5. INI ファイル

 

✔ 特徴

 

保存例 (c#)

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

string dir = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
    "MyCompany", "MyApp");

if (!Directory.Exists(dir)) {
    Directory.CreateDirectory(dir); 
}

string path = Path.Combine(dir, "setting.ini");
WritePrivateProfileString("MainWindow", "Left", this.Left.ToString(), path);

 

 


 

ライセンス

本ページの情報は、特記無い限り下記 MIT ライセンスで提供されます。

The MIT License (MIT)

  Copyright 2026 Kinoshita Hidetoshi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 

 

参考

 


 

変更履歴

2026-09-12 - 新規作成

 

Programming Items トップページ

プライバシーポリシー