ini configuration file to read and write

  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 is          // configuration file path 
. 19          public  static  String iniFile = " the config.ini " ;
 20 is  
21 is          // the API function declaration INI file reader 
22 is          [the DllImport ( " kernel32 " )]
 23 is          Private  static  extern  BOOL the WritePrivateProfileString ( String sectionTop, String Key, String Val, String filePath);
 24          [the 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 is              // create a configuration file, the file exists skip 
32              Files.CreatFile (iniFile);
 33 is          }
 34 is  
35          / //  <Summary> 
36          /// remove profiles
 37 [          ///  </ Summary> 
38 is          public  static  void DeleteIni ()
 39          {
 40              Files.DeleteFile (iniFile);
 41 is         }
 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 = newByte [ 65535 ];
 55              int bufLen = GetPrivateProfileString (Section, Ident, the Default, Buffer, Buffer.GetUpperBound ( 0 ), iniFile);
 56              // must be set to 0 (the default code page) encoding, or can not support Chinese 
57 is              String S = Encoding.GetEncoding ( 0 ) .getString (Buffer);
 58              S s.substring = ( 0 , bufLen);
 59              return s.Trim ();
 60          }
 61 is  
62 is          // write 
63 is          public  static  void WriteIni ( String Section, String Ident,String the Value)
 64          {
 65              IF (! ) the WritePrivateProfileString (Section, Ident, the Value, iniFile)
 66              {
 67                  the throw ( new new ApplicationException ( " written to the configuration file error " ));
 68              }
 69          }
 70  
71 is          // read ini file all Section 
72          public  static List < String > ReadSections ()
 73 is          {
 74              List < String > = SectionsList new 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          // determines whether the present Section 
91 is          public  static  BOOL HasSection ( String sectionTop) {
 92              return ReadSections () the Exists (S => S ==. sectionTop);
 93          }
 94  
95          // read all Ident under Section 
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 }

Guess you like

Origin www.cnblogs.com/eliza209/p/12623549.html