/// <summary> /// 現在のセクション情報をクリアして、ファイルを読み込む。 /// ファイルが存在しなければなにもしない。 /// </summary> /// <param name="filePath"></param> public virtual void Read(string filePath) { if (filePath == null) { throw new ArgumentNullException("filePath"); } RemoveAll(); if (File.Exists(filePath)) { StreamReader sr = null; CSPrivateProfileSection sec = null; string text = null; List <string> comments = new List <string>(); try { sr = new StreamReader(filePath, Encoding.Default); while ((text = sr.ReadLine()) != null) { if (IsComment(text)) { comments.Add(text.Substring(1)); } else if (IsSpace(text)) { comments.Add(null); } else if (IsSection(text)) { string name = text.Substring(1, text.Length - 2); sec = new CSPrivateProfileSection(name, comments); sections.Add(sec); comments.Clear(); } else if (sec != null) { int token = text.IndexOf('='); if (token >= 0) { CSPrivateProfileKeyValue kv = new CSPrivateProfileKeyValue(text.Substring(0, token), text.Substring(token + 1), comments); comments.Clear(); sec.Add(kv); } } } } finally { if (sr != null) { sr.Close(); } } } }
/// <summary> /// 指定したキーの値を取得または設定 /// </summary> public string this[string key] { set { CSPrivateProfileKeyValue kv = GetKeyValue(key); if (kv == null) { kv = new CSPrivateProfileKeyValue(key, value); values.Add(kv); } else { kv.Value = value; } } get { CSPrivateProfileKeyValue kv = GetKeyValue(key); if (kv == null) { return(null); } else { return(kv.Value); } } }
public void SetKeyValue(CSPrivateProfileKeyValue kv) { CSPrivateProfileKeyValue temp = GetKeyValue(kv.Key); if (temp == null) { values.Add(kv); } else { temp.Value = kv.Value; temp.Comments.Clear(); temp.Comments.AddRange(kv.Comments); } }
public void Add(CSPrivateProfileKeyValue kv) { values.Add(kv); }