You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
7.5 KiB
C#

2 years ago
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Rs.Framework
{
public class IniHelper
{
/// <summary>
/// Ini文件完整路径
/// </summary>
public string IniPath { get; set; }
/// <summary>
/// 构造方法
/// </summary>
public IniHelper() { }
/// <summary>
/// 构造方法
/// </summary>
/// <param name="path">Ini文件的完整路径包括文件名</param>
public IniHelper(string path)
{
IniPath = path;
}
#region win32函数
/// <summary>
/// 写操作
/// </summary>
/// <param name="section">Ini文件中的段落</param>
/// <param name="key">Ini文件中的关键字</param>
/// <param name="val">关键字的值</param>
/// <param name="filePath">Ini文件的完整路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
/// <summary>
/// 读操作
/// </summary>
/// <param name="section">Ini文件中的段落</param>
/// <param name="key">Ini文件中的关键字</param>
/// <param name="def">无法读取时的缺省值</param>
/// <param name="retVal">读取的值</param>
/// <param name="size">值的大小</param>
/// <param name="filePath">Ini文件的完整路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
#endregion
/// <summary>
/// Ini文件写入
/// </summary>
/// <param name="section">Ini文件段落</param>
/// <param name="key"></param>
/// <param name="value">值</param>
public void WriteValue(string section, string key, string value)
{
bool b = WritePrivateProfileString(section, key, value, IniPath);
if (!b) throw new Exception("Ini文件写入错误");
}
/// <summary>
/// 读取指定的Section和Ident的值
/// </summary>
/// <param name="section">Ini文件段落</param>
/// <param name="ident">关键字</param>
/// <returns>值</returns>
public string ReadValue(string section, string key)
{
return ReadValue(section, key, "");
}
/// <summary>
/// 读取指定的Section和Ident的值
/// </summary>
/// <param name="section">Ini文件段落</param>
/// <param name="key">关键字</param>
/// <param name="defaultValue">读取失败时的默认值</param>
/// <returns>值</returns>
public string ReadValue(string section, string key, string defaultValue)
{
byte[] buffer = new byte[65535];
int bufLen = GetPrivateProfileString(section, key, defaultValue, buffer, buffer.GetUpperBound(0), IniPath);
string s = Encoding.GetEncoding(0).GetString(buffer);
s = s.Substring(0, bufLen);
return s.Trim();
}
/// <summary>
/// 获取指定Section下的所有Ident
/// </summary>
/// <param name="section"></param>
/// <param name="idents"></param>
public void ReadSection(string section, StringCollection keys)
{
byte[] buffer = new byte[16384];
int bufLen = GetPrivateProfileString(section, null, null, buffer, buffer.GetUpperBound(0), IniPath);
GetStringsFromBuffer(buffer, bufLen, keys);
}
/// <summary>
/// 解析Section
/// </summary>
/// <param name="buffer"></param>
/// <param name="bufLen"></param>
/// <param name="idents"></param>
private static void GetStringsFromBuffer(byte[] buffer, int bufLen, StringCollection keys)
{
keys.Clear();
if (bufLen != 0)
{
int start = 0;
for (int i = 0; i < bufLen; i++)
{
if ((buffer[i] == 0) && ((i - start) > 0))
{
string s = Encoding.GetEncoding(0).GetString(buffer, start, i - start);
keys.Add(s);
start = i + 1;
}
}
}
}
/// <summary>
/// 获取Sections
/// </summary>
/// <param name="sections"></param>
public void ReadSections(StringCollection sections)
{
byte[] buffer = new byte[65535];
int bufLen = GetPrivateProfileString(null, null, null, buffer, buffer.GetUpperBound(0), IniPath);
GetStringsFromBuffer(buffer, bufLen, sections);
}
/// <summary>
/// 获取指定Section下的所有Value
/// </summary>
/// <param name="section"></param>
/// <param name="values"></param>
public void ReadSectionValues(string section, NameValueCollection values)
{
StringCollection idents = new StringCollection();
ReadSection(section, idents);
values.Clear();
foreach (string ident in idents)
{
values.Add(ident, ReadValue(section, ident, ""));
}
}
/// <summary>
/// 清除指定的Section
/// </summary>
/// <param name="section"></param>
public void EraseSection(string section)
{
bool b = WritePrivateProfileString(section, null, null, IniPath);
if (!b) throw new Exception("无法清除Ini文件中的Section");
}
/// <summary>
/// 删除指定Section下的指定键
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
public void DeleteIdent(string section, string key)
{
bool b = WritePrivateProfileString(section, key, null, IniPath);
if (!b) throw new Exception("无法删除指定的Ident");
}
/// <summary>
/// 检查指定的键是否有值
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <returns></returns>
public bool ValueExists(string section, string key)
{
StringCollection keys = new StringCollection();
ReadSection(section, keys);
return keys.IndexOf(key) > -1;
}
/// <summary>
/// 执行完对Ini文件的操作后
/// 调用本方法更新缓冲区,
/// 对于Win9X执行此方法
/// 对于Win NT及更高的系统上则不需执行此方法
/// </summary>
public void UpdateFile()
{
WritePrivateProfileString(null, null, null, IniPath);
}
/// <summary>
/// 验证文件是否存在
/// </summary>
/// <returns></returns>
public bool ExistIniFile
{
get
{
if (string.IsNullOrEmpty(IniPath))
throw new ArgumentException("IniPath");
return File.Exists(IniPath);
}
}
~IniHelper()
{
UpdateFile();
}
}
}