C#实现.ini文件读写操作
程序员文章站
2024-01-15 17:49:58
1、ini文件是什么? 见百度百科:https://baike.baidu.com/item/ini%E6%96%87%E4%BB%B6/9718973?fr=aladdin 2、C#语言实现ini文件的读写操作 此博文为原创,转载请注明出处!!!!! ......
1、ini文件是什么?
见百度百科:
2、C#语言实现ini文件的读写操作
1 /// <summary> 2 /// 配置文件 .ini操作类 3 /// </summary> 4 public class IniFileUtils 5 { 6 /// <summary> 7 /// 写入INI文件 8 /// </summary> 9 /// <param name="section">节点名称[如TypeName]</param> 10 /// <param name="key">键</param> 11 /// <param name="val">值</param> 12 /// <param name="filepath">文件路径</param> 13 /// <returns></returns> 14 [DllImport("kernel32")] 15 private static extern long WritePrivateProfileString(string section, string key, string val, string filepath); 16 /// <summary> 17 /// 读取INI文件 18 /// </summary> 19 /// <param name="section">节点名称</param> 20 /// <param name="key">键</param> 21 /// <param name="def">值</param> 22 /// <param name="retval">stringbulider对象</param> 23 /// <param name="size">字节大小</param> 24 /// <param name="filePath">文件路径</param> 25 /// <returns></returns> 26 [DllImport("kernel32")] 27 private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath); 28 29 /// <summary> 30 /// 写入或更新.ini配置文件属性值 31 /// </summary> 32 /// <param name="section">区域(节点)</param> 33 /// <param name="key">key键属性名称</param> 34 /// <param name="value">key键对应属性值param> 35 /// <param name="path">.ini文件所在路径</param> 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 /// <summary> 46 /// 读取.ini配置文件属性值 47 /// </summary> 48 /// <param name="Section">区域(节点)</param> 49 /// <param name="key">key键属性名称</param> 50 /// <param name="path">.ini文件所在路径</param> 51 /// <returns></returns> 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 }
此博文为原创,转载请注明出处!!!!!
上一篇: 朋友圈优秀模板大全
下一篇: Python显示进度条的方法