ソース自体は前回と殆ど変わってないんですが、簡単な説明を。


名前空間 Siki.Configure


クラス GlobalConfigure

XMLファイルとXML DOMの相互変換を行うクラス。
XML DOMのルートでもある。


クラス ConfigureNode

XMLの要素を表すツリーノード。
このノードは1つの値と複数の子ノードを持つことができる。


クラス ConfigureEventArgs

イベント引数。
今のところ使用しない。


使用例 – フォームの位置とサイズを記録し、次回起動時に復元する

使用する名前空間はSiki.Configure。

1
2
3
4
5
6
7
using System;
using System.Drawing;
using System.Windows.Forms;
using Siki.Configure;

namespace ConfigureSample {
    public partial class Form1 : Form {

GlobalConfigureインスタンスにアクセスするためのプロパティ。

8
9
10
11
        private GlobalConfigure Configure {
            get;
            set;
        }

コンストラクタ。
GlobalConfigureインスタンスの作成。
ここでは設定ファイルにconfigure.xmlを指定。
(別のコンストラクタを使えばディレクトリやXMLのルート要素なども指定できます。)
フォームのLoadとFormClosingイベントにハンドラを設定。

12
13
14
15
16
17
18
        public Form1() {
            InitializeComponent();
            Configure = new GlobalConfigure("configure.xml");
            Load += new EventHandler(Form1_Load);
            FormClosing += new FormClosingEventHandler(Form1_FormClosing);
            //StartPosition = FormStartPosition.Manual;
        }

Loadイベントハンドラで位置とサイズの復元を行う。
FormClosingイベントハンドラで位置とサイズを記録し、設定ファイルに保存する。

19
20
21
22
23
24
25
26
27
28
        private void Form1_Load(object sender, EventArgs e) {
            RestoreLocation();
            RestoreSize();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
            SaveLocation();
            SaveSize();
            Configure.Save();
        }

位置の復元メソッド。
Configureを使用し、連想配列のようにアクセスできる。
返される値はすべてstringなので、他の型への変換は自分で行う必要がある。
指定された要素が存在し、32ビット整数に変換可能ならばtry内で例外は発生しないはず。
以下のコードで
Configure["window"]
Configure["window"]["location"]
Configure["window"]["location"]["x"]
は、いずれもConfigureNodeのインスタンスであり、stringへの変換は暗黙に行われる。

29
30
31
32
33
34
35
36
37
38
39
        public void RestoreLocation() {
            try {
                string strx = Configure["window"]["location"]["x"],
                       stry = Configure["window"]["location"]["y"];
                int x = Convert.ToInt32(strx, 10),
                    y = Convert.ToInt32(stry, 10);
                Location = new Point(x, y);
            } catch {

            }
        }

位置の記録メソッド。
要素に書き込むにはConfigureNode.Valueに代入する必要がある。

40
41
42
43
        public void SaveLocation() {
            Configure["window"]["location"]["x"].Value = Location.X.ToString();
            Configure["window"]["location"]["y"].Value = Location.Y.ToString();
        }

サイズの復元と記録メソッド。
ほぼ同上。

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        public void RestoreSize() {
            try {
                string strw = Configure["window"]["size"]["width"],
                       strh = Configure["window"]["size"]["height"];
                int width = Convert.ToInt32(strw, 10),
                    height = Convert.ToInt32(strh, 10);

                Size = new Size(width, height);
            } catch {

            }
        }

        public void SaveSize() {
            Configure["window"]["size"]["width"].Value = Size.Width.ToString();
            Configure["window"]["size"]["height"].Value = Size.Height.ToString();
        }
    }
}

備考

設定ファイルのディレクトリを明示的に指定しない場合、「~~\ユーザー名\AppData\Roaming\組織名\アプリケーション名\バージョン\」をデフォルトのディレクトリとして扱います。(XP以前では若干違うかも)