1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace XdCxRhDW.Sender
- {
- public static class IniFiles
- {
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
- /// <summary>
- /// 读取INI文件
- /// </summary>
- /// <param name="Section">项名称(如 [TypeName] )</param>
- /// <param name="Key">键</param>
- public static string ReadValue(string iniFile, string section, string Key, string defaultValue = "")
- {
- if (!iniFile.Contains(":"))
- iniFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, iniFile);
- StringBuilder temp = new StringBuilder(50);
- GetPrivateProfileString(section, Key, defaultValue, temp, 500, iniFile);
- return temp.ToString();
- }
- /// <summary>
- /// 读取INI文件
- /// </summary>
- /// <param name="Section">项名称(如 [TypeName] )</param>
- /// <param name="Key">键</param>
- public static T ReadValue<T>(string iniFile, string section, string Key, T defaultValue = default)
- {
- var strValue = ReadValue(iniFile,section,Key);
- if (string.IsNullOrWhiteSpace(strValue))
- return defaultValue;
- var obj = Convert.ChangeType(strValue, typeof(T));
- return (T)obj;
- }
- }
- }
|