C#实现.ini文件读写操作
1、ini文件是什么?
见百度百科:https://baike.baidu.com/item/ini%E6%96%87%E4%BB%B6/9718973?fr=aladdin
2、C#语言实现ini文件的读写操作
1 ///2 /// 配置文件 .ini操作类 3 /// 4 public class IniFileUtils 5 { 6 /// 7 /// 写入INI文件 8 /// 9 /// 节点名称[如TypeName] 10 /// 键 11 /// 值 12 /// 文件路径 13 /// 14 [DllImport("kernel32")] 15 private static extern long WritePrivateProfileString(string section, string key, string val, string filepath); 16 /// 17 /// 读取INI文件 18 /// 19 /// 节点名称 20 /// 键 21 /// 值 22 /// stringbulider对象 23 /// 字节大小 24 /// 文件路径 25 /// 26 [DllImport("kernel32")] 27 private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath); 28 29 /// 30 /// 写入或更新.ini配置文件属性值 31 /// 32 /// 区域(节点) 33 /// key键属性名称 34 /// key键对应属性值param> 35 /// .ini文件所在路径 36 public static void WriteContentValue(string section, string key, string value, string path) 37 { 38 //判断文件是或否存在 39 if (File.Exists(path)) 40 { 41 WritePrivateProfileString(section, key, value, path); 42 } 43 } 44 45 /// 46 /// 读取.ini配置文件属性值 47 /// 48 /// 区域(节点) 49 /// key键属性名称 50 /// .ini文件所在路径 51 /// 52 public static string ReadContentValue(string Section, string key, string path) 53 { 54 StringBuilder temp = new StringBuilder(1024); 55 //判断文件是或否存在 56 if (File.Exists(path)) 57 { 58 GetPrivateProfileString(Section, key, "", temp, 1024, path); 59 } 60 return temp.ToString(); 61 62 } 63 }
此博文为原创,转载请注明出处!!!!!