Original: C # read INI file
Excerpt from: Ituri tutorial Web
Http://www.etoow.com/html/2007-08/1187271505-1.html
Although Microsoft has recommended in Windows to replace the INI file with the registry, but in the actual application, the INI file still has its place, especially now the popularity of green software, more and more programs to save some of their own configuration information in the INI file.
INI file is a text file, consisting of several sections (section), under each parenthesized heading, is a number of keywords (key) and their corresponding values (value)
[Section]
Key=value
VC provides API functions for INI file read and write operations, but Microsoft launched the C # programming language, but there is no corresponding method, the following is a C # INI file read-write class, collected from the Internet, is not the name of the section of the function, the master can add a.
Using system;using system.io;using system.runtime.interopservices;using system.text;using System.Collections;using System.collections.specialized;namespace wuyisky{//<summary>//Inifiles class///</summary> Publ IC class Inifiles {public string filename;//ini file name//Declaration API function for read/write INI file [DllImport ("kernel32")] private static extern bool WritePrivateProfileString (string section, string key, String val, string filePath); [DllImport ("kernel32")] private static extern int getprivateprofilestring (string section, String key, String Def, Byte[] retVal, int size, string filePath); class constructor, pass INI file name public inifiles (string afilename) {//Determine if file exists FileInfo FileInfo = New FileInfo (Afilename); Todo: Figuring out the use of enumerations if ((!fileinfo.exists)) {//| | (Fileattributes.directory in Fileinfo.attributes)) File does not exist, build file System.IO.StreamWriter SW =New System.IO.StreamWriter (Afilename, False, System.Text.Encoding.Default); try {sw. Write ("#表格配置档案"); Sw. Close (); } catch {throw (new ApplicationException ("INI file does not exist")); }}//Must be a full path and cannot be relative to the path FileName = Fileinfo.fullname; }//write INI file public void WriteString (string section, String Ident, String Value) {if (! WritePrivateProfileString (section, Ident, Value, FileName)) {throw (new ApplicationException ("Write INI file ")); }}//read INI file to specify public string ReadString (string section, String Ident, String Default) { byte[] Buffer = new byte[65535]; int buflen = getprivateprofilestring (section, Ident, Default, Buffer, Buffer.getupperbound (0), FileName); You must set the encoding for 0 (the system default code page), or you cannot support Chinese string s = encoding.getencoding (0). GetString (Buffer); s = s.substring (0, Buflen); return S.trim (); }//Read integer public int readinteger (string section, string Ident, int Default) {string intstr = ReadString (section, Ident, Convert.ToString (Default)); try {return Convert.ToInt32 (INTSTR); } catch (Exception ex) {Console.WriteLine (ex. Message); return Default; }}//write integer public void Writeinteger (string section, string Ident, int Value) {Write String (section, Ident, value.tostring ()); }//Read Boolean public bool Readbool (string section, String Ident, bool Default) {try {return Convert.toboolean (ReadString (section, Ident, Convert.ToString (Default))); } catch (Exception ex) {ConSole. WriteLine (ex. Message); return Default; }}//write bool public void Writebool (string section, String Ident, bool Value) {Write String (section, Ident, Convert.ToString (Value)); }//From the INI file, add all the ident in the specified section name to the list public void Readsection (string, StringCollection idents) {byte[] Buffer = new byte[16384]; Idents.clear (); int buflen = getprivateprofilestring (section, NULL, NULL, Buffer, Buffer.getupperbound (0), FileName); The section is parsed Getstringsfrombuffer (Buffer, Buflen, idents); } private void Getstringsfrombuffer (byte[] Buffer, int buflen, StringCollection Strings) {String S.clear (); if (Buflen! = 0) {int start = 0; for (int i = 0; i < Buflen; i++) {if ((buffer[i] = = 0) && ((i-sTart) > 0) {String s = encoding.getencoding (0). GetString (Buffer, start, I-start); Strings.add (s); start = i + 1; }}}}//From the INI file, read all the names of the sections public void Readsections (stringcollectio N sectionlist) {//note: Must be implemented with bytes, StringBuilder can only take the first section byte[] Buffer = new byte[655 35]; int buflen = 0; Buflen = getprivateprofilestring (null, NULL, NULL, Buffer, Buffer.getupperbound (0), FileName); Getstringsfrombuffer (Buffer, Buflen, sectionlist); }//reads all the value of the specified section into the list of public void Readsectionvalues (string, NameValueCollection Values) {StringCollection keylist = new StringCollection (); Readsection (section, keylist); Values.clear (); foreach (string key in Keylist) {Values.add (Key, ReadString (section, Key, "")); }}////reads all the value of the specified section into the list,//public void Readsectionvalues (String, NameValueCollection Values,char splitstring)//{string sectionvalue; String[] Sectionvaluesplit; StringCollection keylist = new StringCollection (); Readsection (section, keylist); Values.clear (); foreach (string key in Keylist)//{//sectionvalue=readstring (section, Key, ""); Sectionvaluesplit=sectionvalue.split (splitstring); Values.add (Key, Sectionvaluesplit[0]. ToString (), sectionvaluesplit[1]. ToString ()); }//}//Clears a section public void Erasesection (string section) {if (! WritePrivateProfileString (section, NULL, NULL, FileName)) {throw (New ApplicationException ("Unable to clear Section "in the INI file)); }}//delete a sectionThe key under public void DeleteKey (string section, String Ident) {writeprivateprofilestring (section, Iden T, NULL, FileName); }//note: For Win9x, it is necessary to implement the Updatefile method to write buffered data to the file//in Win NT, 2000 and XP, are directly written files, no buffering, so there is no need to implement updatefile// After you finish modifying the INI file, you should call this method to update the buffer. public void Updatefile () {writeprivateprofilestring (null, NULL, NULL, FileName); }//Check if a key value under a section exists public bool Valueexists (string section, String Ident) {Stringco Llection idents = new StringCollection (); Readsection (section, idents); Return Idents.indexof (Ident) >-1; }//Ensure the release of resources ~inifiles () {updatefile (); } }}
At present, C # operation of the INI file is basically replaced by the XML file, but I think the INI file read and write is still the basic programming, it must be
C # Read INI file