+++ /dev/null
-using System;\r
-using System.Collections.Generic;\r
-using System.Text;\r
-using System.IO;\r
-using System.Diagnostics;\r
-\r
-namespace FDK\r
-{\r
- /// <summary>\r
- /// 汎用的な .iniファイルを扱う。\r
- /// </summary>\r
- public class CIniFile\r
- {\r
- // プロパティ\r
-\r
- public string strファイル名 \r
- {\r
- get;\r
- private set;\r
- }\r
- public List<CSection> Sections\r
- {\r
- get;\r
- set; \r
- }\r
- public class CSection\r
- {\r
- public string strセクション名 = "";\r
- public List<KeyValuePair<string,string>> listパラメータリスト = new List<KeyValuePair<string, string>>();\r
- }\r
-\r
-\r
- // コンストラクタ\r
-\r
- public CIniFile()\r
- {\r
- this.strファイル名 = "";\r
- this.Sections = new List<CSection>();\r
- }\r
- public CIniFile( string strファイル名 )\r
- :this()\r
- {\r
- this.t読み込み( strファイル名 );\r
- }\r
-\r
-\r
- // メソッド\r
-\r
- public void t読み込み( string strファイル名 )\r
- {\r
- this.strファイル名 = strファイル名;\r
-\r
- StreamReader sr = null;\r
- CSection section = null;\r
- try\r
- {\r
- sr = new StreamReader( this.strファイル名, Encoding.GetEncoding( "Shift_JIS" ) ); // ファイルが存在しない場合は例外発生。\r
-\r
- string line;\r
- while( ( line = sr.ReadLine() ) != null )\r
- {\r
- line = line.Replace( '\t', ' ' ).TrimStart( new char[] { '\t', ' ' } );\r
- if( string.IsNullOrEmpty( line ) || line[ 0 ] == ';' ) // ';'以降はコメントとして無視\r
- continue;\r
-\r
- if( line[ 0 ] == '[' )\r
- {\r
- #region [ セクションの変更 ]\r
- //-----------------------------\r
- var builder = new StringBuilder( 32 );\r
- int num = 1;\r
- while( ( num < line.Length ) && ( line[ num ] != ']' ) )\r
- builder.Append( line[ num++ ] );\r
-\r
- // 変数 section が使用中の場合は、List<CSection> に追加して新しい section を作成する。\r
- if( section != null )\r
- this.Sections.Add( section );\r
-\r
- section = new CSection();\r
- section.strセクション名 = builder.ToString();\r
- //-----------------------------\r
- #endregion\r
-\r
- continue;\r
- }\r
-\r
- string[] strArray = line.Split( new char[] { '=' } );\r
- if( strArray.Length != 2 )\r
- continue;\r
-\r
- string key = strArray[ 0 ].Trim();\r
- string value = strArray[ 1 ].Trim();\r
-\r
- if( section != null && !string.IsNullOrEmpty( key ) && !string.IsNullOrEmpty( value ) )\r
- section.listパラメータリスト.Add( new KeyValuePair<string, string>( key, value ) );\r
- }\r
-\r
- if( section != null )\r
- this.Sections.Add( section );\r
- }\r
- finally\r
- {\r
- if( sr != null )\r
- sr.Close();\r
- }\r
- }\r
- public void t書き出し( string strファイル名 )\r
- {\r
- this.strファイル名 = strファイル名;\r
- this.t書き出し();\r
- }\r
- public void t書き出し()\r
- {\r
- StreamWriter sw = null;\r
- try\r
- {\r
- sw = new StreamWriter( this.strファイル名, false, Encoding.GetEncoding( "utf-16" ) ); // オープン失敗の場合は例外発生。\r
-\r
- foreach( CSection section in this.Sections )\r
- {\r
- sw.WriteLine( "[{0}]", section.strセクション名 );\r
-\r
- foreach( KeyValuePair<string,string> kvp in section.listパラメータリスト )\r
- sw.WriteLine( "{0}={1}", kvp.Key, kvp.Value );\r
- }\r
- }\r
- finally\r
- {\r
- if( sw != null )\r
- sw.Close();\r
- }\r
- }\r
- }\r
-}\r