ini配置文件读写

  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;
 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 bool CheckIniFile(string file)
 30         {
 31             iniFile = file;
 32             //创建配置文件,文件存在则跳过
 33             return Files.CreatFile(iniFile);
 34         }
 35 
 36         /// <summary>
 37         /// 删除配置文件
 38         /// </summary>
 39         public static void DeleteIni()
 40         {
 41             Files.DeleteFile(iniFile);
 42         }
 43 
 44         /// <summary>
 45         /// 打开配置文件
 46         /// </summary>
 47         public static void OpenIni()
 48         {
 49             Files.openFile(iniFile, Files.FileType.TYPE_INI);
 50         }
 51 
 52         //读取
 53         public static string ReadIni(string Section, string Ident, string Default = "")
 54         {
 55             Byte[] Buffer = new Byte[65535];
 56             int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), iniFile);
 57             //必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
 58             string s = Encoding.GetEncoding(0).GetString(Buffer);
 59             s = s.Substring(0, bufLen);
 60             return s.Trim();
 61         }
 62 
 63         //写入
 64         public static void WriteIni(string Section, string Ident, string Value)
 65         {
 66             if (!WritePrivateProfileString(Section, Ident, Value, iniFile))
 67             {
 68                 throw (new ApplicationException("写入配置文件出错"));
 69             }
 70         }
 71 
 72         //删除Section
 73         public static void DeleteSection(string section)
 74         {
 75             if (!WritePrivateProfileString(section, null, null, iniFile))
 76             {
 77                 throw (new ApplicationException("删除节点失败"));
 78             }
 79         }
 80 
 81         //删除Ident
 82         public static void DeleteIdent(string section, string ident)
 83         {
 84             if (!WritePrivateProfileString(section, ident, null, iniFile))
 85             {
 86                 throw (new ApplicationException("删除数据失败"));
 87             }
 88         }
 89 
 90         //读取ini文件的全部Section
 91         public static List<string> ReadSections()
 92         {
 93             List<string> SectionsList = new List<string>();
 94             Byte[] buf = new Byte[65536];
 95             uint len = GetPrivateProfileStringA(null, null, null, buf, buf.Length, iniFile);
 96             int j = 0;
 97             for (int i = 0; i < len; i++)
 98             {
 99                 if (buf[i] == 0)
100                 {
101                     SectionsList.Add(Encoding.Default.GetString(buf, j, i - j));
102 
103                     j = i + 1;
104                 }
105             }
106             return SectionsList;
107         }
108 
109         //判断Section是否存在
110         public static bool HasSection(string section) {
111             return ReadSections().Exists(s => s == section);
112         }
113 
114         //读取Section下的全部Ident
115         public static List<string> ReadIdents(string Section)
116         {
117             List<string> IdentsList = new List<string>();
118             Byte[] buf = new Byte[65536];
119             uint len = GetPrivateProfileStringA(Section, null, null, buf, buf.Length, iniFile);
120             int j = 0;
121             for (int i = 0; i < len; i++)
122             {
123                 if (buf[i] == 0)
124                 {
125                     IdentsList.Add(Encoding.Default.GetString(buf, j, i - j));
126 
127                     j = i + 1;
128                 }
129             }
130             return IdentsList;
131         }
132 
133         //判断Ident是否存在
134         public static bool HasIdent(string section, string ident) {
135             return ReadIdents(section).Exists(i => i == ident);
136         }
137 
138         /// <summary>
139         /// 增加新的配置信息
140         /// </summary>
141         /// <param name="Section"></param>
142         /// <param name="Ident"></param>
143         /// <param name="Value"></param>
144         public static void AddConfigInfo(string Section, string Ident, string Value)
145         {
146             if (!HasSection(Section) || !HasIdent(Section, Ident))
147             {
148                 WriteIni(Section, Ident, Value);
149             }
150         }
151     }
152 }

 

posted @ 2020-04-02 22:39  Yan327  阅读(493)  评论(0编辑  收藏  举报