using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace IS.Tools
{
/// <summary>
/// Ini扩展方法
/// </summary>
public class IniHelper
{
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);//SetValue
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);//GetValue
//ini文件名称
private static string inifilename = "Config.ini";//默认ini文件名
//获取ini文件路径
private static string inifilepath = Directory.GetCurrentDirectory() + "\\" + inifilename;//默认ini文件路径
#region 写INI文件
/// <summary>
/// 写INI文件-完整
/// IniHelper.SetValue("ExecutionTime","Time","2023-8-14 17:05:12","ExecutionTime.ini");
/// </summary>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <param name="val">值</param>
/// <param name="inifilename">ini文件名</param>
public static void SetValue(string section, string key, string val, string inifilename)
{
try
{
string inifilepath = Directory.GetCurrentDirectory() + "\\" + inifilename;
WritePrivateProfileString(section, key, val, inifilepath);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 写INI文件-定制
/// IniHelper.SetValue("Time","2023-8-14 17:05:12","ExecutionTime.ini");
/// </summary>
/// <param name="section"></param>
/// <param name="key">键</param>
/// <param name="val">值</param>
/// <param name="inifilename">ini文件名</param>
public static void SetValue(string key, string val, string inifilename = "RedisConfig.ini")
{
try
{
string inifilepath = Directory.GetCurrentDirectory() + "\\" + inifilename;
WritePrivateProfileString(inifilename.ToLower().Replace(".ini", ""), key, val, inifilepath);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 写INI文件-精简
/// IniHelper.SetValue("Time","2023-8-14 17:05:12");
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
public static void SetValue(string key, string value)
{
try
{
WritePrivateProfileString("CONFIG", key, value, inifilepath);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 读取ini文件
/// <summary>
/// 读取ini文件-完整
/// string s = IniHelper.GetValue("ExecutionTime","Time","2023-8-14 17:05:12","ExecutionTime.ini");
/// </summary>
/// <param name="section">节</param>
/// <param name="key">键</param>
/// <param name="def">缺省值</param>
/// <param name="inifilename">ini文件名</param>
/// <returns>值</returns>
public static string GetValue(string section, string key, string def, string inifilename)
{
try
{
string inifilepath = Directory.GetCurrentDirectory() + "\\" + inifilename;
StringBuilder s = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, s, 1024, inifilepath);
return s.ToString();
}
catch
{
return string.Empty;
}
}
/// <summary>
/// 读取ini文件-定制
/// string s = IniHelper.GetValue("Time","2023-8-14 17:05:12","ExecutionTime.ini");
/// </summary>
/// <param name="key">键</param>
/// <param name="def">默认值</param>
/// <param name="inifilename">ini文件名</param>
/// <returns>值</returns>
public static string GetValue(string key, string def = "", string inifilename = "RedisConfig.ini")
{
try
{
string inifilepath = Directory.GetCurrentDirectory() + "\\" + inifilename;
StringBuilder s = new StringBuilder(1024);
GetPrivateProfileString(inifilename.ToLower().Replace(".ini", ""), key, def, s, 1024, inifilepath);
return s.ToString();
}
catch
{
return string.Empty;
}
}
/// <summary>
/// 读取ini文件-精简
/// string s = IniHelper.GetValue("Time");
/// </summary>
/// <param name="key">键</param>
/// <returns>值</returns>
public static string GetValue(string key)
{
try
{
StringBuilder s = new StringBuilder(1024);
GetPrivateProfileString("CONFIG", key, "", s, 1024, inifilepath);
return s.ToString();
}
catch
{
return string.Empty;
}
}
#endregion
}
}
C#ini文件帮助类
最新推荐文章于 2024-01-23 10:12:20 发布