C#ini文件帮助类

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace IS.Tools
{
    /// 
    /// Ini扩展方法
    /// 
    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文件
        /// 
        /// 写INI文件-完整 
        /// IniHelper.SetValue("ExecutionTime","Time","2023-8-14 17:05:12","ExecutionTime.ini");
        /// 
        /// 节
        /// 键
        /// 值
        /// ini文件名
        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;
            }
        }
        /// 
        /// 写INI文件-定制 
        /// IniHelper.SetValue("Time","2023-8-14 17:05:12","ExecutionTime.ini");
        /// 
        /// 
        /// 键
        /// 值
        /// ini文件名
        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;
            }
        }
        /// 
        /// 写INI文件-精简 
        /// IniHelper.SetValue("Time","2023-8-14 17:05:12");
        /// 
        /// 键
        /// 值
        public static void SetValue(string key, string value)
        {
            try
            {
                WritePrivateProfileString("CONFIG", key, value, inifilepath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 读取ini文件
        /// 
        /// 读取ini文件-完整
        /// string s = IniHelper.GetValue("ExecutionTime","Time","2023-8-14 17:05:12","ExecutionTime.ini");
        /// 
        /// 节
        /// 键
        /// 缺省值
        /// ini文件名
        /// 
        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;
            }
        }
        /// 
        /// 读取ini文件-定制
        /// string s = IniHelper.GetValue("Time","2023-8-14 17:05:12","ExecutionTime.ini");
        /// 
        /// 键
        /// 默认值
        /// ini文件名
        /// 
        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;
            }
        }
        /// 
        /// 读取ini文件-精简
        /// string s = IniHelper.GetValue("Time");
        /// 
        /// 键
        /// 
        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)