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

winform编程——做过ini配置文件创建文件、添加配置项,修改配置项,删除配置项
(c#.net2005   windows窗体程序)
我查阅了相关资料和请教了一些人
写了一个ini类
在用的时候我
先写了创建ini的命令
添加配置项的命令
运行
创建ini文件

创建了以后再添加配置项,就不行了,也别说修改了

如果有大虾做过ini配置文件,还请多多帮忙
回复你的邮件,我把我的项目发过来你帮偶看看,求助!


------解决方案--------------------
已回复,需要注意类接口方法的定义。
------解决方案--------------------
public sealed class IniFile
{
private string path;

/// <summary>
/// 实例初始化为指定路径的INI文件。
/// </summary>
/// <param name= "path "> INI文件路径。 </param>
public IniFile(string path)
{
this.path = path;
}

/// <summary>
/// 获取INI文件的路径。
/// </summary>
public string Path
{
get
{
return path;
}
}

/// <summary>
/// 读取指定小节下指定条目的字符串。
/// </summary>
/// <param name= "sectionName "> 欲在其中查找条目的小节名称。这个字串不区分大小写。 </param>
/// <param name= "keyName "> 欲获取的项名或条目名。这个字串不区分大小写。 </param>
/// <param name= "defaultValue "> 指定的条目没有找到时返回的默认值。 </param>
/// <returns> 指定小节下指定条目的字符串。 </returns>
/// <remarks> 如果sectionName为null,则返回所有小节的列表,如果keyName为null,指定小节所有项的列表。 </remarks>
public string ReadString(string sectionName,string keyName,string defaultValue)
{
const int MAXSIZE = 255;
StringBuilder temp = new StringBuilder(MAXSIZE);
GetPrivateProfileString(sectionName, keyName, defaultValue, temp, 255, this.path);
return temp.ToString();
}

public void WriteString(string sectionName, string keyName, string value)
{
WritePrivateProfileString(sectionName, keyName, value, this.path);
}

public int ReadInteger(string sectionName, string keyName, int defaultValue)
{
return GetPrivateProfileInt(sectionName, keyName, defaultValue,this.path);
}

public void WriteInteger(string sectionName, string keyName, int value)
{
WritePrivateProfileString(sectionName, keyName, value.ToString(), this.path);
}

public bool ReadBoolean(string sectionName, string keyName, bool defaultValue)
{
int temp = defaultValue ? 1 : 0;
int result = GetPrivateProfileInt(sectionName, keyName, temp, this.path);
return (result == 0 ? false : true);
}

public void WriteBoolean(string sectionName, string keyName, bool value)
{
string temp = value ? "1 " : "0 ";
WritePrivateProfileString(sectionName, keyName, temp, this.path);
}

/// <summary>
/// 删除这个项现有的字串。
/// </summary>
/// <param name= "sectionName "> 要设置的项名或条目名。这个字串不区分大小写。 </param>
/// <param name= "keyName "> 要删除的项名或条目名。这个字串不区分大小写。 </param>
public void DeleteKey(string sectionName,string keyName)
{
WritePrivateProfileString(sectionName, keyName, null, this.path);
}

/// <summary>