C# API读写ini配置文件
2011-05-23 16:13
423 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Collections; /////////////////////////////////////////////////////////////////////// // INI文件是文本文件,由若干节(section)组成,在每个带方括号的标题下面, //是若干个关键词(key)及其对应的值(Value) //[Section] //Key=Value ////////////////////////////////////////////////////////////////////// namespace iniHelper { public class DAL { [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 def, StringBuilder retVal, int size, string filePath); #region 字段与属性 private string _iniFileFullPath; public string ls_iniFileFullPath { set { _iniFileFullPath = value; } get { return _iniFileFullPath; } } #endregion /// <summary> /// 写入数据到INI文件中 /// <PARAM name="iniSection">ini节点名称</PARAM> /// <PARAM name="iniKey">iniKEY名称</PARAM> /// <PARAM name="iniValue">写入的值</PARAM> /// </summary> public void Write(string iniSection, string iniKey, string iniValue) { WritePrivateProfileString(iniSection, iniKey, iniValue, this.ls_iniFileFullPath); } /// <summary> ///从ini文件中读取数据 /// <PARAM name="iniSection">ini节点名称</PARAM> /// <PARAM name="iniKey">iniKEY名称</PARAM> /// <returns>指定ini节点或iniKEY的值</returns> /// </summary> public string Read(string iniSection, string iniKey) { StringBuilder resultValue = new StringBuilder(65535); int i = GetPrivateProfileString(iniSection, iniKey, "", resultValue, 65535, this.ls_iniFileFullPath); return resultValue.ToString(); } /// <summary> /// 删除指定节点 /// </summary> /// <param name="section">要删除的节点名</param> /// <returns>删除成功或节点不存在,返回值都为真</returns> public bool DeleteSection(string section) { bool flag = false;//标志 try { if (section.Trim().Length <= 0) {//找不到节点 flag = false; } else { if (WritePrivateProfileString(section, null, null, this.ls_iniFileFullPath) == 0) { flag = false; } else { flag = true; } } } catch { flag = false; } return flag; } /// <summary> /// 删除指定节点下的KEY值 /// </summary> /// <param name="section">指定节点</param> /// <param name="key">指定节点下的key</param> /// <returns>删除成功或不存在时都返回true</returns> public bool DeleteKey(string section, string key) { bool flag = false; try { if (section.Trim().Length <= 0 || key.Trim().Length <= 0) { flag = false; } else { if (WritePrivateProfileString(section, key, null, this.ls_iniFileFullPath) == 0) { flag = false; } else { flag = true; } } } catch { flag = false; } return flag; } } } such as: [UserInfo] Smtp=smtp.qq.com UserInfo 是节点名(Section) Smtp 是关键值(keys) smtp.qq.com 是对应值(value) 感觉就和XML文件一样的····只不过··他没有根节点,,瀑布式的子节点而已。 name:5+x
相关文章推荐
- C#读写ini配置文件
- C# 文件的一些基本操作(转)//用C#读写ini配置文件
- C# 调用win32的api 读写 ini 文件
- c#通用配置文件读写类(xml,ini,json)
- 用C#读写ini配置文件
- c#通用配置文件读写类与格式转换(xml,ini,json)
- C#_读写ini配置文件
- c# 读写INI配置文件 张宇轩
- C# 读写ini配置文件的类
- 使用C#读写ini配置文件
- C#读写INI配置文件
- 用C#读写ini配置文件完整范例
- C#常用操作类库二(ini配置文件读写)
- 用C#读写ini配置文件
- C#读写INI配置文件(转)
- C#编程如何编写的自动读写配置文件.ini格式
- 在C#中读写INI配置文件
- C# 读写ini配置文件(.net/SQL技术交流群206656202 入群需注明博客园)
- c#用DLL,API读写删INI.TXT文件的例子
- 用C#读写ini配置文件