C#中读写INI文件
在网上找了关于ini文件读写方法,还是没有找到ini文件中有一个Section多个Key的读写情况,在一篇C++文章中得到点提示操作如下:
1.创建ini文件读写类:
2.测试INI文件读写:
结果:
[Section1]
Key1=Key1's Value
[Section2]
Key2=Key2's Value
[UseFileName]
file1=value1
file2=value2
file3=value3
file4=value4
1.创建ini文件读写类:
using System.Runtime.InteropServices;
using System.Text;
namespace INIDemo
{
/// <summary>
/// 读写ini文件的类
/// 调用kernel32.dll中的两个API:WritePrivateProfileString,GetPrivateProfileString
/// 来实现对ini 文件的读写。
/// INI文件是文本文件,
/// 由若干节(section)组成,
/// 在每个带括号的标题下面,
/// 是若干个关键词(key)及其对应的值(value)
/// 例如:
/// [Section]
/// Key=value
/// </summary>
public class IniFile
{
/// <summary>
/// ini文件名称(带路径)
/// </summary>
public string filePath;
/// 声明读写INI文件的API函数
[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);
/// <summary>
/// 类的构造函数
/// </summary>
/// <param name="INIPath">INI文件名</param>
public IniFile(string INIPath)
{
filePath = INIPath;
}
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">Key</param>
/// <param name="value">value</param>
public void WriteInivalue(string Section, string Key, string value)
{
WritePrivateProfileString(Section, Key, value, this.filePath);
}
/// <summary>
/// 读取INI文件指定部分
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">Key</param>
/// <returns>String</returns>
public string ReadInivalue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(1024);
int i = GetPrivateProfileString(Section, Key, "读取错误", temp, 1024, this.filePath);
return temp.ToString();
}
}
}
using System.Text;
namespace INIDemo
{
/// <summary>
/// 读写ini文件的类
/// 调用kernel32.dll中的两个API:WritePrivateProfileString,GetPrivateProfileString
/// 来实现对ini 文件的读写。
/// INI文件是文本文件,
/// 由若干节(section)组成,
/// 在每个带括号的标题下面,
/// 是若干个关键词(key)及其对应的值(value)
/// 例如:
/// [Section]
/// Key=value
/// </summary>
public class IniFile
{
/// <summary>
/// ini文件名称(带路径)
/// </summary>
public string filePath;
/// 声明读写INI文件的API函数
[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);
/// <summary>
/// 类的构造函数
/// </summary>
/// <param name="INIPath">INI文件名</param>
public IniFile(string INIPath)
{
filePath = INIPath;
}
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">Key</param>
/// <param name="value">value</param>
public void WriteInivalue(string Section, string Key, string value)
{
WritePrivateProfileString(Section, Key, value, this.filePath);
}
/// <summary>
/// 读取INI文件指定部分
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">Key</param>
/// <returns>String</returns>
public string ReadInivalue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(1024);
int i = GetPrivateProfileString(Section, Key, "读取错误", temp, 1024, this.filePath);
return temp.ToString();
}
}
}
2.测试INI文件读写:
//写入ini文件
private void button1_Click(object sender, EventArgs e)
{
string filePath = Path.GetFullPath(@"demo.ini");
IniFile iniFile = new IniFile(filePath);
//在一个section中写入一个key
iniFile.WriteInivalue("Section1", "Key1", "Key1's Value");
iniFile.WriteInivalue("Section2", "Key2", "Key2's Value");
//在一个section中写入多个key
string[] fileNames = new string[] { "file1", "file2", "file3", "file4" };
string[] values = new string[] { "value1", "value2", "value3", "value4" };
for (int i = 0; i < 4; i++)
{
iniFile.WriteInivalue("UseFileName", fileNames[i], values[i]);
}
}
//读取ini文件
private void button2_Click(object sender, EventArgs e)
{
string filePath = Path.GetFullPath(@"demo.ini");
IniFile iniFile = new IniFile(filePath);
//读取单个section的单个值
string singleValue1 = null;
string singleValue2 = null;
singleValue1 = iniFile.ReadInivalue("Section1", "Key1");
singleValue2 = iniFile.ReadInivalue("Section2", "Key2");
MessageBox.Show(singleValue1 + " " + singleValue2);
//读取单个section的多个值
string[] fileNames = new string[] { "file1", "file2", "file3", "file4" };
ArrayList values = new ArrayList();
for (int i = 0; i < 4; i++)
{
values.Add(iniFile.ReadInivalue("UseFileName", fileNames[i]));
}
int nCount = values.Count;
string multiValues = null;
for (int i = 0; i < nCount; i++)
{
multiValues += values[i].ToString() + " ";
}
MessageBox.Show(multiValues);
}
private void button1_Click(object sender, EventArgs e)
{
string filePath = Path.GetFullPath(@"demo.ini");
IniFile iniFile = new IniFile(filePath);
//在一个section中写入一个key
iniFile.WriteInivalue("Section1", "Key1", "Key1's Value");
iniFile.WriteInivalue("Section2", "Key2", "Key2's Value");
//在一个section中写入多个key
string[] fileNames = new string[] { "file1", "file2", "file3", "file4" };
string[] values = new string[] { "value1", "value2", "value3", "value4" };
for (int i = 0; i < 4; i++)
{
iniFile.WriteInivalue("UseFileName", fileNames[i], values[i]);
}
}
//读取ini文件
private void button2_Click(object sender, EventArgs e)
{
string filePath = Path.GetFullPath(@"demo.ini");
IniFile iniFile = new IniFile(filePath);
//读取单个section的单个值
string singleValue1 = null;
string singleValue2 = null;
singleValue1 = iniFile.ReadInivalue("Section1", "Key1");
singleValue2 = iniFile.ReadInivalue("Section2", "Key2");
MessageBox.Show(singleValue1 + " " + singleValue2);
//读取单个section的多个值
string[] fileNames = new string[] { "file1", "file2", "file3", "file4" };
ArrayList values = new ArrayList();
for (int i = 0; i < 4; i++)
{
values.Add(iniFile.ReadInivalue("UseFileName", fileNames[i]));
}
int nCount = values.Count;
string multiValues = null;
for (int i = 0; i < nCount; i++)
{
multiValues += values[i].ToString() + " ";
}
MessageBox.Show(multiValues);
}
结果:
[Section1]
Key1=Key1's Value
[Section2]
Key2=Key2's Value
[UseFileName]
file1=value1
file2=value2
file3=value3
file4=value4
懒惰使人的思维变得迟钝