c#怎么读写.ini配置文件?下面itmemo小编给大家分享一段(c)Sharp读写配置文件代码。
/// <summary> /// 读写配置项文件*.ini /// </summary> public class ReadAndWriteIni { // 声明INI文件的写操作函数 WritePrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 声明INI文件的读操作函数 GetPrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath); public static bool Write(string section, string key, string value, string sPath) { // section=配置节,key=键名,value=键值,path=路径 long bOK = WritePrivateProfileString(section, key, value, sPath); return true; } public static string Read(string section, string key, string defaultVal, string sPath) { // 每次从ini中读取多少字节 System.Text.StringBuilder temp = new System.Text.StringBuilder(255); // section=配置节,key=键名,defaultVal=当没有读到值时返回模式值,temp=上面,path=路径; GetPrivateProfileString(section, key, defaultVal, temp, 255, sPath); return temp.ToString(); } }
直接复制以上代码新建类调用即可,一般数据量不大的文件都用.ini读写配置项,所以这个还是比较实用的!
正在阅读:
c#读写.ini配置文件源码05-29
六年级儿童节作文200字左右09-03
三月情人节温馨的告白语3篇09-28
2022年山东莱芜初级经济师报名时间及入口(7月26日-8月16日)04-24
2018年广西柳州市中考物理真题及答案(Word版)06-27
2022年度黑龙江哈尔滨师范大学参加“黑龙江人才周”引进人才公告【134人】11-09
日记200字08-08
2018年福建三明市泰宁县全科医生特设岗位招聘通告07-14
这一天,我站在了舞台上作文800字08-24