C# ini文件读写 实例

来源:互联网 发布:软件编程程序培训机构 编辑:程序博客网 时间:2024/05/17 03:40

ini文件一般用于保存当前运行的程序或者一些临时的配置属性的文件。也有时用于保存一定的数据以便于临时或者配置上的需要。

文本格式如下:

 

[Section1 Name] ---------用 []括起来,其包含多个keyKeyName1=value1 ------格式是 Key=value。KeyName2=value2 
... [Section2 Name] KeyName1=value1 KeyName2=value2
其中有专门读写ini文件的windows方法:
 [DllImport("kernel32")]
// 写入ini文件操作section,key,value
 private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);
 [DllImport("kernel32")]
// 读取ini文件操作section,key,和返回的keyvalue
 private static extern int GetPrivateProfileString(string section,string key, string def, StringBuilder retVal,int size, string filePath);
C#操作时代码如下: