/******************************
* 说明: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.AutoUpdate
{
///
/// Ini 文件操作类
///
public static class IniUtil
{
private static readonly string absolutelyPath = Application.StartupPath + "\\";
private static readonly string mainMenuConfigPath = absolutelyPath + "MenuConfig.ini";
///
/// 写入INI文件
///
/// 节点名称[如[TypeName]]
/// 键
/// 值
/// 文件路径
///
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
///
/// 读取INI文件
///
/// 节点名称
/// 键
/// 值
/// stringbulider对象
/// 字节大小
/// 文件路径
///
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
///
/// 获取某个指定节点(Section)中所有KEY和Value
///
/// 节点名称
/// 返回值的内存地址,每个之间用\0分隔
/// 内存大小(characters)
/// Ini文件
/// 内容的实际长度,为0表示没有内容,为nSize-2表示内存大小不够
[DllImport("kernel32")]
private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpszReturnBuffer, int nSize, string lpFileName);
///
/// 说明:写入Ini文件(以键值对的形式存放)
/// 创建人:龚宇超
/// 创建日期:2017-07-22
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// 键
/// 值
///
public static void Write(string key, string value)
{
try
{
//根据INI文件名设置要写入INI文件的节点名称
//此处的节点名称完全可以根据实际需要进行配置
string filePath = mainMenuConfigPath;
if (!File.Exists(filePath))
{
File.Create(filePath);
}
string fileName = Path.GetFileNameWithoutExtension(filePath);
WritePrivateProfileString(fileName, key, value, filePath);
}
catch (Exception ex)
{
}
}
///
/// 说明:通过Key获取Value值
/// 创建人:龚宇超
/// 创建日期:2017-07-22
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// 键
/// 值
public static string Read(string key)
{
string value = string.Empty;
try
{
//判读INI文件是否存在
string filePath = 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;
}
///
/// 说明:写入Ini文件(以键值对的形式存放)
/// 创建人:龚宇超
/// 创建日期:2017-07-22
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// 键
/// 值
///
public static void Write(string iniName, string key, string value)
{
try
{
//根据INI文件名设置要写入INI文件的节点名称
//此处的节点名称完全可以根据实际需要进行配置
string filePath = Path.Combine(new string[] { absolutelyPath + iniName });
if (!File.Exists(filePath))
{
File.Create(filePath);
}
string fileName = Path.GetFileNameWithoutExtension(filePath);
WritePrivateProfileString(fileName, key, value, filePath);
}
catch (Exception ex)
{
}
}
///
/// 说明:通过Key获取Value值
/// 创建人:龚宇超
/// 创建日期:2017-07-22
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// 键
/// 值
public static string Read(string iniName, string key)
{
string value = string.Empty;
try
{
//判读INI文件是否存在
string filePath = Path.Combine(new string[] { 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;
}
///
/// 说明:通过Key获取Value值
/// 创建人:龚宇超
/// 创建日期:2017-07-22
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// 键
/// 值
public static string Read(string iniName, string mainKey, string listKey)
{
string value = string.Empty;
try
{
//判读INI文件是否存在
string filePath = Path.Combine(new string[] { 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;
}
///
/// 获取INI文件中指定节点(Section)中的所有条目(key=value形式)
///
/// Ini文件
/// 节点名称
/// 指定节点中的所有项目,没有内容返回string[0]
public static Dictionary GetSectionKeys(string iniFile, string category)
{
Dictionary result = new Dictionary();
try
{
string filePath = Path.Combine(new string[] { 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;
}
}
}