ENGLISH 意见建议 网站地图 网站帮助
广泛智力汇聚   高效成果传播   先进机制培育
联盟首页  |  协同开发  |  开放源码库  |  安全告警  |  开源导航  |  文档中心  |  服务支持  |  共创论坛  |  关于联盟


注册会员 网站帮助
    您的位置 »
    今天是: 2010年11月22日    
项目搜索

完全匹配   
开源软件
软件分类表
新发布软件
其它网站镜像
代码片断
协同开发
文档
论坛
寻求协助
热点项目
站点状态
编译工厂

联系我们
关于联盟

代码片段库:
查看代码片段

浏览 | 提交新的代码片段 | 创建代码包

ini文件读写类

类型:
Class
类别:
File Management
许可证:
GNU General Public License
语言:
C#
 
描述:
ini文件读写类

该代码片段的版本系列:

片段ID 下载版本 提交时间 提交人 删除
48711.02010-02-07 13:19guocong89

点击"下载版本"来下载该代码片段.


最新版本的代码片段: 1.0


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace DaBaoKu
{
    /// <summary>
    /// 读写ini配置文件类
    /// </summary>
    class IniFile
    {
        #region 成员变量

        public string filePath = "";    //文件路径
        static private uint MaxBufferSize = 32767;//缓存大小
        #endregion

        #region 导入API

        [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);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, IntPtr retVal, int size, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileInt(string section, string key, int def, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileSection(string section,IntPtr lpReturnedString,uint nSize,string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer,uint nSize,string filepath);

        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数.
        /// </summary>
        /// <PARAM name="INIPath"></PARAM>
        public IniFile(string filename)
        {
            filePath = System.IO.Path.GetFullPath(filename);
        }
        #endregion
  
        #region 读取数据
        
        public string GetString(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.filePath);
            return temp.ToString();
        }
       
        public int GetInt(string Section, string Key)
        {
            return GetPrivateProfileInt(Section, Key, 0,  this.filePath);
        }
       
        public List<KeyValuePair<string,string>> GetValueSetList(string Section)  //读取一段内的所有数据
        {
            List<KeyValuePair<string, string>> retval;
            string[] keyValuePairs;
            string key, value;
            int equalSignPos;

            //申请空间
            IntPtr ptr = Marshal.AllocCoTaskMem((int)IniFile.MaxBufferSize);
            
            try
            {
                int len = GetPrivateProfileSection(Section,ptr,MaxBufferSize,filePath);
                keyValuePairs = ConvertNullSeperatedStringToStringArray(ptr, len);
            }
            finally
            {
                Marshal.FreeCoTaskMem(ptr);
            }

            //输出结果
            retval = new List<KeyValuePair<string, string>>(keyValuePairs.Length);
            for (int i = 0; i < keyValuePairs.Length; ++i)
            {
                equalSignPos = keyValuePairs[i].IndexOf('=');
                key = keyValuePairs[i].Substring(0, equalSignPos);
                value = keyValuePairs[i].Substring(equalSignPos + 1,keyValuePairs[i].Length - equalSignPos - 1);
                retval.Add(new KeyValuePair<string, string>(key, value));
            }
            return retval;
        }

        public string[] GetSectionNames()   //获得所有段名
        {
            string[] retval;
            int len;
            IntPtr ptr = Marshal.AllocCoTaskMem((int)IniFile.MaxBufferSize);

            try
            {
                len = GetPrivateProfileSectionNames(ptr,IniFile.MaxBufferSize, filePath);
                retval = ConvertNullSeperatedStringToStringArray(ptr, len);
            }
            finally
            {
                Marshal.FreeCoTaskMem(ptr);
            }
            return retval;
        }

        public string[] GetKeyNames(string Section) //获得一段内所有关键码
        {
            int len;
            string[] retval;
            IntPtr ptr = Marshal.AllocCoTaskMem((int)IniFile.MaxBufferSize);

            try
            {
                len = GetPrivateProfileString(Section, null, null, ptr, (int)IniFile.MaxBufferSize, filePath);
                retval = ConvertNullSeperatedStringToStringArray(ptr, len);
            }
            finally
            {
                Marshal.FreeCoTaskMem(ptr);
            }
            return retval;
        }
       
        private static string[] ConvertNullSeperatedStringToStringArray(IntPtr ptr, int valLength) //转换字符串指针空间为字符串数组
        {
            string[] retval;
            if (valLength == 0)
            {
                retval = new string[0];
            }
            else
            {
                string buff = Marshal.PtrToStringAuto(ptr, valLength - 1);
                retval = buff.Split('
		

提交新版本

如果您修改了一个代码片段并且觉得很应该让别人共享,您可以把这作为这个代码片段的最新版本提交上来.


联盟团体会员
合作伙伴
© 共创软件联盟 版权所有
联盟服务条款 | 联盟隐私权规则 | 联系我们
电话: (8610)68313388-5949 | 传真: (8610)88377936
京ICP备05056057号