2
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?

More than 3 years have passed since last update.

Excelアドイン(VSTO)でユーザー設定を保存する方法

Last updated at Posted at 2021-01-17

開発環境

  • Windows 10 Home
  • Microsoft Visual Studio Community 2019 Version 16.8.4
  • Microsoft .NET Framework Version 4.8.03752

設定ファイル(app.config or user.config)の読み書き

ユーザが変更したUIの設定などを記録するために利用
(参考:Visual Studioでアプリケーションの設定を保存する

1.ソリューションエクスプローラーでプロジェクトを右クリック「プロパティ」を選択
2. 「設定」で保存したいパラメータ(名前、型、スコープ、初期値)を追加
setting.png
3. アドインのLoad関数(Ribbon_アドイン名_Load())のなかで設定を読み込み
4. アドインのClose関数が無いので、値が変更されたら随時保存する

Properties.Settings setting = Properties.Settings.Default;

// Write
setting.checkBox = checkBox.Checked;
setting.Save();

// Read
checkBox.Checked = setting.checkBox;

アップグレード時の対応

設定ファイルはバージョンごとにフォルダが作られ保存されるため、バージョンアップをすると初期値が設定されてしまう。
以下は、直前のバージョンの設定ファイルを引き継ぐ処理。

事前に、パラメータを追加して、設定を読み込む前(Load関数など)に実施しておく。

名前 種類 スコープ
IsUpgrated bool (不問) False
if (!Properties.Settings.Default.IsUpgrated)
{
    Properties.Settings.Default.Upgrade();
    Properties.Settings.Default.IsUpgrated = true;
}

設定ファイルの場所の確認

  • 参照に追加:System.Configuration
using System.Configuration;

Configuration config = ConfigurationManager.OpenExeConfiguration(
    ConfigurationUserLevel.PerUserRoamingAndLocal
);
string path = config.FilePath;
2
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
2
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?