C# INI操作代码

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Text;
using System.ComponentModel;
namespace BasicFunctionStudy
{
    public class INIHelper
    {
        static string FilePath = null;
        /// <summary>
        /// 当读取不到值得时候缺省值
        /// </summary>
        static string ReadDefaultValue = string.Empty;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="filePath">INI路径eg:@"C:\test.ini"</param>
        public INIHelper(string filePath)
        {
            FilePath = filePath;
        }
        /// <summary>
        /// 声明INI文件的写操作函数
        /// </summary>
        /// <param name="section">段落名称</param>
        /// <param name="key">关键字</param>
        /// <param name="val">关键字对应的值</param>
        /// <param name="filePath">路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        /// <summary>
        /// 声明INI文件的读操作函数
        /// </summary>
        /// <param name="section">段落名称</param>
        /// <param name="key">关键字</param>
        /// <param name="def">无法读取时候时候的缺省数值</param>
        /// <param name="retVal">读取数值</param>
        /// <param name="size">数值的大小></param>
        /// <param name="filePath">路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        /// <summary>
        /// 写入INI
        /// eg:_iniHelper.WriteValue("测试", "Name", "Chang Weihua");
        /// </summary>
        /// <param name="Section">段落名称</param>
        /// <param name="Key">关键字</param>
        /// <param name="Value">关键字对应的值</param>
        public void WriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, FilePath);
        }
        /// <summary>
        /// 读取INI
        /// </summary>
        /// <param name="Section">段落名称</param>
        /// <param name="Key">关键字</param>
        /// <returns>读取值</returns>
        public string ReadValue(string Section, string Key)
        {
            StringBuilder _valueBuilder = new StringBuilder(500);
            GetPrivateProfileString(Section, Key, ReadDefaultValue, _valueBuilder, 500, FilePath);
            return _valueBuilder.ToString();
        }
        /// <summary>
        /// 读取INI
        /// </summary>
        /// <param name="Section">段落名称</param>
        /// <param name="Key">关键字</param>
        /// <param name="defaultValue">当根据KEY读取不到值得时候缺省值</param>
        /// <returns></returns>
        public string ReadValue(string Section, string Key, string defaultValue)
        {
            StringBuilder _valueBuilder = new StringBuilder(500);
            GetPrivateProfileString(Section, Key, defaultValue, _valueBuilder, 500, FilePath);
            return _valueBuilder.ToString();
        }
        /// <summary>
        /// 检查INI文件路径是否存在
        /// </summary>
        /// <returns></returns>
        public bool Exist()
        {
            if (!string.IsNullOrEmpty(FilePath))
            {
                return File.Exists(FilePath);
            }
            return false;
        }
        /// <summary>
        /// 将对象保存在ini
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="Section">段落名称</param>
        /// <param name="t">类型</param>
        public void WriteValue<T>(string Section, T t) where T : class
        {
            IDictionary<string, string> _property = GetDisplayName<T>();
            foreach (KeyValuePair<string, string> entry in _property)
            {
                object _value = typeof(T).InvokeMember(entry.Key, BindingFlags.GetProperty, null, t, null);
                Trace.WriteLine(_value);
                if (_value != null)
                    WriteValue(Section, entry.Value, _value.ToString());
            }
        }
        private IDictionary<string, string> GetDisplayName<T>()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            var type = typeof(T);
            var props = type.GetProperties ();
            foreach (var prop in props) {
                var attrs = prop.GetCustomAttributes (typeof(DisplayNameAttribute), true);
                foreach (DisplayNameAttribute attr in attrs) {
                    //yield return new KeyValuePair<string, string> (prop.Name, attr.DisplayName);//错误代码,IDictionary 未实现迭代器
                    dict.Add (prop.Name, attr.DisplayName);
                }
            }
            return dict;
        }
    }
    public class Person
    {
        [DisplayName("姓名")]
        public string Name{ get; set;}
        [DisplayName("年龄")]
        public int Age{ get; set;}
        [DisplayName("地址")]
        public string Address{ get; set;}
    }
}


知识共享许可协议
《C# INI操作代码》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     179.18ms