C#で書いた.NET Framework/Mono用INIファイル読み込み/保存ライブラリです。
機能
現在対応している機能の一覧です。 部分的に対応しているものも含みます。
- セクションが指定されていないキー=値のペアの読み込み
- ;および#で記述されるコメント行の読み込み
- セクション名・キー名の前後に空白が含まれている形式の読み込み
- セクション名・キー名の比較に任意のIEqualityComparer<string>を指定可能
- 任意のConverter<string, TOutput>を指定することで任意の型に変換した上でキーの値を取得可能
- 非Windows環境でも使用可(GetPrivateProfile*系のWin32 APIは使用していません)
ダウンロード
0.14以降はMIT X11ライセンスでのリリースとなります。 0.14より前のバージョンについては特に使用条件を定めていません。
- 0.2x
- サイズ
- 9.1kB
- sha1sum
- fdba7b1ab022fdb02a881e8b1ef2b865bae3d538
- サイズ
- 10.4kB
- sha1sum
- 58fc7a223323a427df1bfcf6b32ad5310e1af9a7
- サイズ
- 10.1kB
- sha1sum
- 769b942f8f8f199e15b26adb2ad8aeb5bc6fec11
- 0.1x
- サイズ
- 9.8kB
- sha1sum
- 15ee5e4c2069fbbf5454c2bded2fbd7919bd70bc
- サイズ
- 9.7kB
- sha1sum
- 33d03580f755d82470dc476310262718b4c985a6
- サイズ
- 8.1kB
- sha1sum
- 3a2b0302ad231bd178a66a5c8377c857f7ba2d53
- サイズ
- 8.1kB
- sha1sum
- 14511088c10204392eb35f73dc4841cf779d5cee
- サイズ
- 8.1kB
- sha1sum
- f77596084a6285757cb4b1d2dae33e6a8315e10f
- サイズ
- 5.9kB
- sha1sum
- 4ecf75452987be5fa695cdc42270eed3f0cfc7cc
- 上記以前のバージョン
- TundereBirdに含まれているものを使ってください
不具合・要望・質問等
不具合の報告、ライブラリに対する要望や提案、使い方の質問などは掲示板へどうぞ。
サンプル
本ライブラリを使用したサンプルアプリケーションSimpleMailerもあわせてご覧ください。
読み込み
Sambaの設定ファイルと同様の形式のテキストを読み込み、値を表示する例。
using System;
using System.IO;
using Smdn.Formats.Ini;
class IniSample {
static void Main(string[] args)
{
var ini = @"
[global]
# Change this to the workgroup/NT-domain name your Samba server will part of
workgroup = WORKGROUP
# This will prevent nmbd to search for NetBIOS names through DNS.
dns proxy = no
# The specific set of interfaces / networks to bind to
# This can be either the interface name or an IP address/netmask;
# interface names are normally preferred
; interfaces = 127.0.0.0/8 eth0
[printers]
comment = All Printers
browseable = no
path = /var/spool/samba
printable = yes
guest ok = no
read only = yes
create mask = 0700
";
var config = IniDocument.Load(new StringReader(ini));
Console.WriteLine("[global]");
// キーの値をstringで取得
Console.WriteLine("workgroup: {0}", config["global"]["workgroup"]);
// yesならtrue、それ以外はfalseとして値を取得
Console.WriteLine("dns proxy: {0}", config["global"].Get("dns proxy", (val) => val == "yes"));
// キーが存在しない場合のデフォルトを指定して取得
Console.WriteLine("interfaces: {0}", config["global"].Get("interfaces", "(default interface)"));
// 同じくデフォルトを指定してbool型の値として取得
Console.WriteLine("wins support: {0}", config["global"].Get("wins support", false, (val) => val == "yes"));
// 条件にマッチするセクションを取得(config["printers"]と同じ)
var printers = config.Find((section) => section.Name == "printers");
Console.WriteLine("[printers]");
// デフォルトでは大文字小文字を無視してキー名を比較する
// (Loadメソッドの引数で比較に使用するIEqualityComparer<string>を指定可能)
Console.WriteLine("read only: {0}", printers.Get("READ Only", (val) => val == "yes"));
Console.WriteLine("guest ok: {0}", printers.Get("guest ok", (val) => val == "yes"));
Console.WriteLine("path: {0}", printers.Get("path", (val) => new DirectoryInfo(val)));
Console.WriteLine("create mask: 0b{0}", Convert.ToString(printers.Get("create mask", (val) => Convert.ToInt32(val, 8)), 2));
}
}
実行結果
[global] workgroup: WORKGROUP dns proxy: False interfaces: (default interface) wins support: False [printers] read only: True guest ok: False path: /var/spool/samba create mask: 0b111000000
書き込み
desktop.iniを作成、ファイルとコンソールに出力する例。
using System;
using Smdn.Formats.Ini;
class IniSample {
static void Main(string[] args)
{
var desktopIni = new IniDocument();
// .ShellClassInfoセクションを作成・取得
var shellClassInfo = desktopIni[".ShellClassInfo"];
// それぞれのキーと値を設定
shellClassInfo["InfoTip"] = "hoge";
shellClassInfo["IconFile"] = @"%SystemRoot%\system32\mydocs.dll";
shellClassInfo["IconIndex"] = "-101";
desktopIni.Save(@"D:\test\desktop.ini");
desktopIni.Save(Console.Out);
}
}
コンソールおよびD:\test\desktop.iniの出力結果
[.ShellClassInfo] InfoTip=hoge IconFile=%SystemRoot%\system32\mydocs.dll IconIndex=-101
変更履歴
0.22 (2014-04-23)
- 修正・改善
- .NET Framework 2.0のサポートを廃止
- その他機能上の変更はなし
0.21 (2013-07-15)
- 修正・改善
- ファイルへの上書きを行う場合に以前の内容が残ったままとなる可能性があったのを修正
0.20 (2013-02-07)
- 修正・改善
- 変更可能になっていたIniDocument.DefaultComparerをreadonlyに変更
0.19 (2010-12-11)
- 修正・改善
- 内部で使用している正規表現にRegexOptions.Compiled, RegexOptions.CultureInvariantを追加
- 内部実装を若干簡略化
0.18 (2010-04-25)
- 機能追加
- .NET Framework 2.0, 3.5, 4の各バージョン用のプロジェクトファイルを追加
0.17 (2010-03-22)
- 修正・改善
- StringComparison.Ordinal*の動作を期待していた箇所でInvariantCulture*を使用していたのを修正
0.16 (2010-01-23)
- TundereBirdバージョンアップに伴うリリース
0.15 (2009-12-16)
- 機能追加
- 任意の型変換用デリゲート(Converter<string, TOutput>)を指定して値を取得できるようにした
- セクション名・キー名の比較に任意のIEqualityComparer<string>を指定できるようにした
- セクションを削除できるようにした
- 修正・改善
- ファイルの上書き時に以前の内容が残る場合がある不具合を修正
- 個々のセクションの間に改行を入れて保存するように修正
- クラス名を変更
0.14 (2009-06-13)
- ライセンスをMIT X11に変更
0.13 (2008-04-27)
- TundereBirdバージョンアップに伴うリリース
0.12 (2008-04-07)
- TundereBirdバージョンアップに伴うリリース
0.11 (2008-03-10)
- 行頭に空白およびタブを含むセクション・エントリを読み込めるようにした
0.10 (2008-03-06)
- 初版
動作状況
以下の環境で動作することを確認済みです。
- .NET Framework 4
- .NET Framework 3.5
- Mono 3.4.0
- Mono 2.11.3
- Mono 2.10.8
- Mono 2.8.2
対応予定
今後対応する予定の機能です。
- 元のフォーマット・コメントを維持した保存機能
- インデント・改行などのフォーマットを指定した保存機能