ab56a9bcf7
SVN-Revision: r240
248 lines
9.2 KiB
C#
248 lines
9.2 KiB
C#
/******************************
|
|
* 说明:Ini 文件操作类
|
|
* 创建人:龚宇超
|
|
* 创建日期:2017-07-22
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
******************************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Runtime.InteropServices;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Lskj.Util
|
|
{
|
|
/// <summary>
|
|
/// Ini 文件操作类
|
|
/// </summary>
|
|
public static class IniHelper
|
|
{
|
|
/// <summary>
|
|
/// 写入INI文件
|
|
/// </summary>
|
|
/// <param name="section">节点名称[如[TypeName]]</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">stringbulider对象</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>
|
|
/// 获取某个指定节点(Section)中所有KEY和Value
|
|
/// </summary>
|
|
/// <param name="lpAppName">节点名称</param>
|
|
/// <param name="lpReturnedString">返回值的内存地址,每个之间用\0分隔</param>
|
|
/// <param name="nSize">内存大小(characters)</param>
|
|
/// <param name="lpFileName">Ini文件</param>
|
|
/// <returns>内容的实际长度,为0表示没有内容,为nSize-2表示内存大小不够</returns>
|
|
[DllImport("kernel32")]
|
|
private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpszReturnBuffer, int nSize, string lpFileName);
|
|
/// <summary>
|
|
/// <para>说明:写入Ini文件(以键值对的形式存放)</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-22</para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <param name="value">值</param>
|
|
/// <returns></returns>
|
|
public static void Write(string key, string value)
|
|
{
|
|
try
|
|
{
|
|
//根据INI文件名设置要写入INI文件的节点名称
|
|
//此处的节点名称完全可以根据实际需要进行配置
|
|
string filePath = PubUtil.MainMenuConfigPath;
|
|
if (!File.Exists(filePath))
|
|
{
|
|
File.Create(filePath);
|
|
}
|
|
string fileName = Path.GetFileNameWithoutExtension(filePath);
|
|
WritePrivateProfileString(fileName, key, value, filePath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:通过Key获取Value值</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-22</para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns>值</returns>
|
|
public static string Read(string key)
|
|
{
|
|
string value = string.Empty;
|
|
try
|
|
{
|
|
//判读INI文件是否存在
|
|
string filePath = PubUtil.MainMenuConfigPath;
|
|
if (File.Exists(filePath))
|
|
{
|
|
StringBuilder temp = new StringBuilder(1024);
|
|
string fileName = Path.GetFileNameWithoutExtension(filePath);
|
|
GetPrivateProfileString(fileName, key, "", temp, 1024, filePath);
|
|
|
|
return temp + "";
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
return value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:写入Ini文件(以键值对的形式存放)</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-22</para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <param name="value">值</param>
|
|
/// <returns></returns>
|
|
public static void Write(string iniName, string key, string value)
|
|
{
|
|
try
|
|
{
|
|
//根据INI文件名设置要写入INI文件的节点名称
|
|
//此处的节点名称完全可以根据实际需要进行配置
|
|
string filePath = Path.Combine(new string[] { PubUtil.AbsolutelyPath + iniName });
|
|
if (!File.Exists(filePath))
|
|
{
|
|
File.Create(filePath);
|
|
}
|
|
string fileName = Path.GetFileNameWithoutExtension(filePath);
|
|
WritePrivateProfileString(fileName, key, value, filePath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:通过Key获取Value值</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-22</para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns>值</returns>
|
|
public static string Read(string iniName, string key)
|
|
{
|
|
string value = string.Empty;
|
|
try
|
|
{
|
|
//判读INI文件是否存在
|
|
string filePath = Path.Combine(new string[] { PubUtil.AbsolutelyPath + iniName });
|
|
if (File.Exists(filePath))
|
|
{
|
|
StringBuilder temp = new StringBuilder(1024);
|
|
string fileName = Path.GetFileNameWithoutExtension(filePath);
|
|
GetPrivateProfileString(fileName, key, "", temp, 1024, filePath);
|
|
|
|
return temp + "";
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
return value;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:通过Key获取Value值</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-22</para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns>值</returns>
|
|
public static string Read(string iniName, string mainKey, string listKey)
|
|
{
|
|
string value = string.Empty;
|
|
try
|
|
{
|
|
//判读INI文件是否存在
|
|
string filePath = Path.Combine(new string[] { PubUtil.AbsolutelyPath + iniName });
|
|
if (File.Exists(filePath))
|
|
{
|
|
StringBuilder temp = new StringBuilder(1024);
|
|
string fileName = Path.GetFileNameWithoutExtension(filePath);
|
|
GetPrivateProfileString(mainKey, listKey, "", temp, 1024, filePath);
|
|
|
|
return temp + "";
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
return value;
|
|
}
|
|
/// <summary>
|
|
/// 获取INI文件中指定节点(Section)中的所有条目(key=value形式)
|
|
/// </summary>
|
|
/// <param name="iniFile">Ini文件</param>
|
|
/// <param name="section">节点名称</param>
|
|
/// <returns>指定节点中的所有项目,没有内容返回string[0]</returns>
|
|
public static Dictionary<string, string> GetSectionKeys(string iniFile, string category)
|
|
{
|
|
Dictionary<string, string> result = new Dictionary<string, string>();
|
|
try
|
|
{
|
|
string filePath = Path.Combine(new string[] { PubUtil.AbsolutelyPath + iniFile });
|
|
if (File.Exists(filePath))
|
|
{
|
|
byte[] buffer = new byte[2048];
|
|
GetPrivateProfileSection(category, buffer, 2048, filePath);
|
|
String[] tmp = Encoding.Default.GetString(buffer).Trim('\0').Split('\0');
|
|
foreach (String entry in tmp)
|
|
{
|
|
string[] v = entry.Split('=');
|
|
result.Add(v[0], v[1]);
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
return result;
|
|
}
|
|
|
|
}
|
|
} |