usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.IO;usingSystem.Reflection;usingSystem.Runtime.InteropServices;usingSystem.Text;namespaceCSharpUtilHelpV2
{/// <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>publicclassINIToolV2
{staticstringFilePath =null;/// <summary>/// 当读取不到值得时候缺省值/// </summary>staticstringReadDefaultValue =string.Empty;/// <summary>/// 构造函数/// </summary>/// <param name="filePath">INI路径eg:@"C:\test.ini"</param>publicINIToolV2(stringfilePath)
{
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")]PRivatestaticexternlongWritePrivateProfileString(stringsection,stringkey,stringval,stringfilePath);/// <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")]privatestaticexternintGetPrivateProfileString(stringsection,stringkey,stringdef, StringBuilder retVal,intsize,stringfilePath);/// <summary>/// 写入INI/// eg:_iniHelper.WriteValue("测试", "Name", "YanZhiwei");/// </summary>/// <param name="Section">段落名称</param>/// <param name="Key">关键字</param>/// <param name="Value">关键字对应的值</param>publicvoidWriteValue(stringSection,stringKey,stringValue)
{
WritePrivateProfileString(Section, Key, Value, FilePath);
}/// <summary>/// 读取INI/// </summary>/// <param name="Section">段落名称</param>/// <param name="Key">关键字</param>/// <returns>读取值</returns>publicstringReadValue(stringSection,stringKey)
{
StringBuilder _valueBuilder =newStringBuilder(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>publicstringReadValue(stringSection,stringKey,stringdefaultValue)
{
StringBuilder _valueBuilder =newStringBuilder(500);
GetPrivateProfileString(Section, Key, defaultValue, _valueBuilder, 500, FilePath);return_valueBuilder.ToString();
}/// <summary>/// 检查INI文件路径是否存在/// </summary>/// <returns></returns>publicboolExist()
{if(!string.IsNullOrEmpty(FilePath))
{returnFile.Exists(FilePath);
}returnfalse;
}/// <summary>/// 将对象保存在ini/// </summary>/// <typeparam name="T">泛型</typeparam>/// <param name="Section">段落名称</param>/// <param name="t">类型</param>publicvoidWriteValue<T>(stringSection, T t)whereT :class{
IDictionary<string,string> _property = ReflectionToolV2.GetDisplayName<T>();foreach(KeyValuePair<string,string> entryin_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());
}
}
}
}
.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; }.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; }测试代码
代码效果: