第一步使用INI类
public class INIClass
{
public string inipath;
//配置文件写入函数
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
//配置文件读取函数
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public INIClass(string INIPath)
{
inipath = INIPath;
}
//创建这个类的写入ini文件的方法
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.inipath);
}
//创建这个类的读取ini文件的方法
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
return temp.ToString();
}
调用方法:
INIClass INIConfig = new INIClass(Application.StartupPath + "\\config.ini");
使用写入:
INIConfig.IniWriteValue("Setting", "ExcelSavePath", "D:\\test");
使用读取:
INIConfig.IniReadValue("Setting", "ExcelSavePath")