C # gets all the sections in the INI file to get all the keys under the section

Source: Internet
Author: User
Tags read ini file

usingSystem;usingSystem.Text;usingSystem.IO;usingSystem.Runtime.InteropServices;usingSystem.Collections.Generic; Public classOperateinifile {Private StaticString FilePath ="";#regionAPI function Declaration[DllImport ("kernel32")]Private Static extern LongWritePrivateProfileString (stringSectionstringKey,stringValstringFilePath);
     //overload that requires calling GetPrivateProfileString [DllImport ("kernel32", EntryPoint ="getprivateprofilestring")] Private Static extern LongGetPrivateProfileString (stringSectionstringKey,stringDef, StringBuilder RetVal,intSizestringFilePath); [DllImport ("kernel32", EntryPoint ="getprivateprofilestring")] Private Static extern UINTGetprivateprofilestringa (stringSectionstringKey,stringDef, byte[] RetVal,intSizestringFilePath); #endregion Public Staticlist<string>readsections () {returnreadsections (FilePath); } Public Staticlist<string> Readsections (stringinifilename) {List<string> result =Newlist<string>(); Byte[] BUF=Newbyte[65536]; UINTLen = Getprivateprofilestringa (NULL,NULL,NULL, buf, buf. Length, Inifilename); intj =0; for(inti =0; i < Len; i++) if(Buf[i] = =0) {result. ADD (Encoding.Default.GetString (buf, J, I-j)); J= i +1; } returnresult; } Public Staticlist<string>Readkeys (String sectionname) {returnReadkeys (sectionname, FilePath); } Public Staticlist<string> Readkeys (stringSectionName,stringinifilename) {List<string> result =Newlist<string>(); Byte[] BUF=Newbyte[65536]; UINTLen = Getprivateprofilestringa (sectionname,NULL,NULL, buf, buf. Length, Inifilename); intj =0; for(inti =0; i < Len; i++) if(Buf[i] = =0) {result. ADD (Encoding.Default.GetString (buf, J, I-j)); J= i +1; } returnresult; } Public Static voidSetFilePath (String filepath) {filepath=filepath; } #regionRead INI file Public Static stringReadinidata (stringSection,stringKey,stringNotext) { returnreadinidata (section, Key, Notext, FilePath); }
Public Static stringReadinidata (stringSection,stringKey,stringNotext,stringIniFilePath) { if(File.exists (IniFilePath)) {StringBuilder temp=NewStringBuilder (1024x768); GetPrivateProfileString (section, Key, Notext, temp,1024x768, IniFilePath); returntemp. ToString (); } ElsereturnString.Empty; } #endregion #regionWrite INI file Public Static BOOLWriteinidata (stringSection,stringKey,stringValue) { returnwriteinidata (section, Key, Value, FilePath); } Public Static BOOLWriteinidata (stringSection,stringKey,stringValue,stringIniFilePath) { if(File.exists (IniFilePath)) {LongOpstation =writeprivateprofilestring (section, Key, Value, IniFilePath); if(Opstation = =0) return false; Elsereturn true; } Elsereturn false; } #endregion }

C # gets all the sections in the INI file to get all the keys under the section

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.