C# ini 文件解析库
2015-07-01 10:55
746 查看
ini解析
因为需要使用C#进行ini 文件读写操作, 看到官方说解析ini文件需要使用xml读写库。感觉太过麻烦。因为先前有MFC 读写INI文件的经验,感觉API非常简单,所以稍微封装了一下。代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.InteropServices; namespace IniFile { class IniParser { [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 default_var, StringBuilder retVal, int buff_size, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileInt(string section, string key, int default_var, string filePath); private string _file_path; public IniParser() { _file_path = Directory.GetCurrentDirectory()+"\\" +System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".ini"; } public IniParser(string t_filename) { if (string.IsNullOrEmpty(t_filename)) { _file_path = System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".ini"; return; } else { _file_path = t_filename; } } public bool writeData(string t_section, string t_key, string t_data) /// if write data sucess , return true ,otherwise return false { long i_result = WritePrivateProfileString(t_section, t_key, t_data, _file_path); return (i_result!=0?true:false); } public string readStringData(string t_section, string t_key, string t_default = "" ) /// if success , value would return , otherwise empty string return { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(t_section, t_key, t_default, temp, 255, this._file_path); return temp.ToString(); } public int readIntData(string t_section, string t_key, int t_default =0) { int i_result = GetPrivateProfileInt(t_section, t_key, t_default, this._file_path); return i_result; } } }
相关文章推荐
- android 代码实现控件之间的间距
- [Android]在代码里运行另一个程序的方法
- 肯特·贝克:改变人生的代码整理魔法
- 网页恶意代码的预防
- 高手写的Tracer-Flash代码调试类代码下载
- Flex字符串比较 还有Flex字符串操作
- CSS代码缩写技巧
- 非主流Q-zOne代码代码搜集第1/2页
- 修复mysql数据库
- CreateWeb.vbs 代码
- Lua中编译执行代码相关的函数详解
- ASP Cookies操作的详细介绍与实例代码
- perl数据库添加、删除、更新、查询操作例子
- 总结的5个C#字符串操作方法分享
- C#操作XML文件实例汇总
- C#中父窗口和子窗口之间控件互操作实例
- C#操作PowerPoint的方法
- SQL语句 操作全集 学习mssql的朋友一定要看
- 更有效率的css代码编写第1/3页
- nodejs中的fiber(纤程)库详解