.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 |
.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();
}
}
}
"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() は不要(ありません)のようです。アプリケーション起動時に自動的に読み込みをやってくれているようです。
上記のようにとても簡単に機能を実現可能です。
保存例 (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);
保存例 (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));
保存例 (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);
保存例 (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 ライセンスで提供されます。
| 2026-09-12 | - | 新規作成 |