C#读写INI文件的方法

2019-12-26 13:35:00王冬梅
  • //  string[] sectionValueSplit;  //  StringCollection KeyList = new StringCollection(); 
  • //  ReadSection(Section, KeyList);  //  Values.Clear(); 
  • //  foreach (string key in KeyList)  //  { 
  • //    sectionValue=ReadString(Section, key, "");  //    sectionValueSplit=sectionValue.Split(splitString); 
  • //    Values.Add(key, sectionValueSplit[0].ToString(),sectionValueSplit[1].ToString());  //  } 
  • //}  //清除某个Section 
  • public void EraseSection(string Section)  { 
  • if (!WritePrivateProfileString(Section, null, null, FileName))  { 
  • throw (new ApplicationException("无法清除Ini文件中的Section"));  } 
  • }  //删除某个Section下的键 
  • public void DeleteKey(string Section, string Ident)  { 
  • WritePrivateProfileString(Section, Ident, null, FileName);  } 
  • //Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件  //在Win NT, 2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile 
  • //执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。  public void UpdateFile() 
  • {  WritePrivateProfileString(null, null, null, FileName); 
  • }  //检查某个Section下的某个键值是否存在 
  • public bool ValueExists(string Section, string Ident)  { 
  • StringCollection Idents = new StringCollection();  ReadSection(Section, Idents); 
  • return Idents.IndexOf(Ident) > -1;  } 
  • //确保资源的释放  ~IniFiles() 
  • {  UpdateFile(); 
  • }  }