C#で書いた.NET Framework/Mono用INIファイル読み込み/保存ライブラリです。
現在対応している機能の一覧です。 部分的に対応しているものも含みます。
0.14以降はMIT X11ライセンスでのリリースとなります。 0.14より前のバージョンについては特に使用条件を定めていません。
本ライブラリを使用したサンプルアプリケーションTundereBirdもあわせてご覧ください。
Sambaの設定ファイルと同様の形式のテキストを読み込み、値を表示する例。
using System; using System.IO; using Smdn.Formats.Ini; class IniSample { public 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)); } }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を指定可能) 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 { public 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); } }
[.ShellClassInfo] InfoTip=hoge IconFile=%SystemRoot%\system32\mydocs.dll IconIndex=-101