首页 > 代码库 > [C#]对于INI操作代码

[C#]对于INI操作代码

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 = http://www.mamicode.com/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 = http://www.mamicode.com/typeof(T).InvokeMember(entry.Key, BindingFlags.GetProperty, null, t, null);                Trace.WriteLine(_value);                if (_value != null)                    WriteValue(Section, entry.Value, _value.ToString());            }        }    }}
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>测试代码

image

image

代码效果:

image