[C#]对于INI操作代码_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > [C#]对于INI操作代码

[C#]对于INI操作代码

 2014/6/17 11:27:45  楚人游子  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.IO;usingSystem.Reflection;usingSystem.Runtime.InteropServices;usingSystem.Text;namespaceCSharpUtilHelpV2{///<summary>///基于.NET2.0的INI工具类///参考:///http://www.cnblogs
  • 标签:C# 代码 操作
class="csharpcode">using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace CSharpUtilHelpV2
{
    /// <summary>
    /// 基于.NET 2.0的INI工具类
    /// 参考:
    /// http://www.cnblogs.com/leelike/archive/2011/01/27/1946061.html
    /// http://www.cnblogs.com/zzyyll2/archive/2007/11/06/950584.html    
    /// </summary>
    public class INIToolV2
    {
        static string FilePath = null;
        /// <summary>
        /// 当读取不到值得时候缺省值
        /// </summary>
        static string ReadDefaultValue = string.Empty;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="filePath">INI路径eg:@"C:\test.ini"</param>
        public INIToolV2(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", "YanZhiwei");
        /// </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 = ReflectionToolV2.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());
            }
        }
    }
}

测试代码

image

image

代码效果:

image

上一篇: 亚马逊手机还未问世已有24万应用,较去年翻了3倍 下一篇: 没有下一篇了!
发表评论
用户名: 匿名