日期:2014-05-19  浏览次数:20889 次

怎么修改INI文件中标题下面的键值
[NEWS]
m1=01
m2=01
怎么把m1修改为m3.

------解决方案--------------------
添个m3=01
……
------解决方案--------------------
添个m3=01
删个m1=01
------解决方案--------------------
#region 读写ini文件 API定义
[DllImport( "kernel32.dll ", EntryPoint = "WritePrivateProfileStringA ")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport( "kernel32.dll ", EntryPoint = "GetPrivateProfileStringA ")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
#endregion

/// <summary>
/// 写INI文件
/// </summary>
/// <param name= "Section "> 配置目录 </param>
/// <param name= "Key "> 参数 </param>
/// <param name= "Value "> 值 </param>
/// <param name= "IniPath "> 路径 </param>
public static bool IniWriteValue(string Section, string Key, string Value, string IniPath)
{
try
{
WritePrivateProfileString(Section, Key, Value, IniPath);
return true;
}
catch
{
return false;
}

}
/// <summary>
/// 读INI文件
/// </summary>
/// <param name= "Section "> 配置目录 </param>
/// <param name= "Key "> 参数 </param>
/// <param name= "IniPath "> 路径 </param>
/// <returns> 返回值 </returns>
public static string IniReadValue(string Section, string Key, string IniPath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, " ", temp, 255, IniPath);
return temp.ToString();
}
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace IniFiles
{
public abstract class CustomIniFile
{
public CustomIniFile(string AFileName)
{
FFileName = AFileName;
}
private string FFileName;
public string FileName
{
get { return FFileName; }
}
public virtual bool SectionExists(string Section)
{
List <string> vStrings = new List <string> ();
ReadSections(vStrings);
return vStrings.Contains(Section);
}
public virtual bool ValueExists(string Section, string Ident)
{
List <string> vStrings = new List <string> ();
ReadSection(Section, vStrings);
return vStrings.Contains(Ident);
}
public abstract string ReadString(string Section, string Ident, string Default);
public abstract bool WriteString(string Section, string Ident, string Value);
public abstract bool ReadSectionValues(string Section, List <string> Strings);
public abstract bool ReadSection(string Section, List <string> Strings);
public abstract bool ReadSections(List <st