网站技术
当前目录:首页 >> 新闻中心 >> 网站技术

C#-INI文件操作类 INIHelper

发布人:卓伟软件
发布日期:2014-05-07 00:58:06
浏览次数:1466
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace XiaoFeng
{
    /// <summary>

    /// INI 操作类

   ///作者:jacky

    ///QQ:7092734

   ///网址:http://www.d369.net

    ///E-mail:jacky@d369.net

    /// </summary>
    public class INIHelper
    {
        #region 声明读写INI文件的API函数
        [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileIntA", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true)]
        private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);

        [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileSectionsNamesA", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true)]
        private static extern int GetPrivateProfileSectionsNames(byte[] lpszReturnBuffer, int nSize, string lpFileName);

        [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringA", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true)]
        private static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
        
        [DllImport("KERNEL32")]
        private static extern int GetPrivateProfileString(string lpAppName, string lpszKey, string lpString, Byte[] lpStruct, int uSizeStruct, string lpFileName);
        
        [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStructA", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true)]
        private static extern int GetPrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);

        [DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileSectionsA", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true)]
        private static extern int WritePrivateProfileSections(string lpAppName, string lpString, string lpFileName);

        [DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringA", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true)]
        private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);

        [DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStructA", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true)]
        private static extern int WritePrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);


        #endregion

        /// <summary>
        /// 无参构造器
        /// </summary>
        public INIHelper() { }
        /// <summary>
        /// 无参构造器
        /// </summary>
        /// <param name="path">文件路径 绝对路径</param>
        public INIHelper(string path)
        {
            if (System.IO.File.Exists(path))
            {
                this.fPath = path;
            }
            else
            {
                this.ErrorMessage = "您输入的配置文件不存在.";
                this.hPath = path;
            }
        }

        #region 属性
        /// <summary>
        /// 文件路径
        /// </summary>
        private string _hPath = "";
        /// <summary>
        /// 文件路径
        /// </summary>
        public string hPath { get { return _hPath; } set { _hPath = value; } }
        /// <summary>
        /// 文件路径 绝对路径
        /// </summary>
        private string _fPath = "";
        /// <summary>
        /// 文件路径 绝对路径
        /// </summary>
        public string fPath { get { return _fPath; } set { _fPath = value; } }
        /// <summary>
        /// 错误信息
        /// </summary>
        private string _ErrorMessage = "";
        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMessage { get { return _ErrorMessage; } set { _ErrorMessage = value; } }
        #endregion

        #region 方法
        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="section">段落,格式[]</param>
        /// <param name="key">键</param>
        /// <param name="iValue">值</param>
        public void WriteValue(string section, string key, string iValue)
        {
            if (this.fPath == "")
            {
                System.IO.File.Create(this.hPath);
                this.fPath = this.hPath;
            }
            WritePrivateProfileString(section, key, iValue, this.fPath);
        }

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">段落,格式[]</param>
        /// <param name="key">键</param>
        /// <returns>返回的键值</returns>
        public string ReadValue(string section, string key)
        {
            if (this.fPath == "") return "";
            StringBuilder temp = new StringBuilder(255);

            int i = GetPrivateProfileString(section, key, "", temp, 255, this.fPath);
            return temp.ToString();
        }

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">段落,格式[]</param>
        /// <param name="key">键</param>
        /// <returns>返回byte类型的section组或键值组</returns>
        public byte[] ReadValues(string section, string key)
        {
            byte[] temp = new byte[255];

            int i = GetPrivateProfileString(section, key, "", temp, 255, this.fPath);
            return temp;
        }
        #endregion
    }
}
Copyright © 2010 - 2015 Zhuovi. All Rights Reserved
联系电话:15837199978  E-Mail:service@zhuovi.com  在线QQ:点击这里给我发消息 点击这里给我发消息  备案号:豫ICP备14022191号
郑州网站建设 | 河南网站建设 | 郑州软件 | 河南软件 | 办公软件 | 移动短信 | 注册域名 | 虚拟空间,网站空间 | 群发短信 | 网站优化 | 网站推广 | 优化技术 | 郑州作网站 | 河南作网站
在线咨询 返回顶部