1 using LogTool; 2 using System; 3 using System.IO; 4 using System.Linq; 5 using System.Runtime.InteropServices; 6 using System.Text; 7 using System.Threading.Tasks; 8 using FileTool; 9 using System.Collections.Generic; 10 11 namespace IniTool 12 { 13 /// <summary> 14 /// ini配置文件读写类 15 /// </summary> 16 public class ini_RW 17 { 18 //配置文件保存路径 19 public static string iniFile = "config.ini"; 20 21 //声明读写INI文件的API函数 22 [DllImport("kernel32")] 23 private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath); 24 [DllImport("kernel32")] 25 private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath); 26 [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")] 27 private static extern uint GetPrivateProfileStringA(string section, string key,string def, Byte[] retVal, int size, string filePath); 28 29 public static void CheckIniFile() 30 { 31 //创建配置文件,文件存在则跳过 32 Files.CreatFile(iniFile); 33 } 34 35 /// <summary> 36 /// 删除配置文件 37 /// </summary> 38 public static void DeleteIni() 39 { 40 Files.DeleteFile(iniFile); 41 } 42 43 /// <summary> 44 /// 打开配置文件 45 /// </summary> 46 public static void OpenIni() 47 { 48 Files.openFile(iniFile, Files.FileType.TYPE_INI); 49 } 50 51 //读取 52 public static string ReadIni(string Section, string Ident, string Default) 53 { 54 Byte[] Buffer = new Byte[65535]; 55 int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), iniFile); 56 //必须设定0(系统默认的代码页)的编码方式,否则无法支持中文 57 string s = Encoding.GetEncoding(0).GetString(Buffer); 58 s = s.Substring(0, bufLen); 59 return s.Trim(); 60 } 61 62 //写入 63 public static void WriteIni(string Section, string Ident, string Value) 64 { 65 if (!WritePrivateProfileString(Section, Ident, Value, iniFile)) 66 { 67 throw (new ApplicationException("写入配置文件出错")); 68 } 69 } 70 71 //读取ini文件的全部Section 72 public static List<string> ReadSections() 73 { 74 List<string> SectionsList = new List<string>(); 75 Byte[] buf = new Byte[65536]; 76 uint len = GetPrivateProfileStringA(null, null, null, buf, buf.Length, iniFile); 77 int j = 0; 78 for (int i = 0; i < len; i++) 79 { 80 if (buf[i] == 0) 81 { 82 SectionsList.Add(Encoding.Default.GetString(buf, j, i - j)); 83 84 j = i + 1; 85 } 86 } 87 return SectionsList; 88 } 89 90 //判断Section是否存在 91 public static bool HasSection(string section) { 92 return ReadSections().Exists(s => s == section); 93 } 94 95 //读取Section下的全部Ident 96 public static List<string> ReadIdents(string Section) 97 { 98 List<string> IdentsList = new List<string>(); 99 Byte[] buf = new Byte[65536]; 100 uint len = GetPrivateProfileStringA(Section, null, null, buf, buf.Length, iniFile); 101 int j = 0; 102 for (int i = 0; i < len; i++) 103 { 104 if (buf[i] == 0) 105 { 106 IdentsList.Add(Encoding.Default.GetString(buf, j, i - j)); 107 108 j = i + 1; 109 } 110 } 111 return IdentsList; 112 } 113 114 //判断Ident是否存在 115 public static bool HasIdent(string section, string ident) { 116 return ReadIdents(section).Exists(i => i == ident); 117 } 118 119 /// <summary> 120 /// 增加新的配置信息 121 /// </summary> 122 /// <param name="Section"></param> 123 /// <param name="Ident"></param> 124 /// <param name="Value"></param> 125 public static void AddConfigInfo(string Section, string Ident, string Value) 126 { 127 if (!HasSection(Section) || !HasIdent(Section, Ident)) 128 { 129 WriteIni(Section, Ident, Value); 130 } 131 } 132 } 133 }
ini配置文件读写
猜你喜欢
转载自www.cnblogs.com/eliza209/p/12623549.html
今日推荐
周排行