INI file simple package

xiaoxiao2021-03-06  109

Such implementation of the simple package of the INI file can extract all items corresponding to all Sections and Section in the file.

Using system; using system.runtime.interopservices; using system.collections;

Namespace TestiniFile {///

/// inIfile summary description. /// public class IniFile {[DllImport ( "kernel32")] private static extern long WritePrivateProfileString (string section, string key, string val, string filePath); [DllImport ( "kernel32")] private static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport ( "kernel32")] private static extern int GetPrivateProfileString (string section, string key, string def, byte [] retVal, int Size, String FilePath;

Public inIfile () {} public inifile (string filename) {inIfilename = filename; sectionsArray = new arraylist ();}

// returns and reads all Sections public void ReadSections () {byte [] byteBuffer = new byte [11024]; // set Buffer length string tmpStr = null; if (GetPrivateProfileString (null, null, null, byteBuffer, 1024, InIfileName)> 0) {IF (ABYTE == 0) {if (tmpstr == null) {Break;} else {sectionarray.add (tmpstr); tmpstr = null;}} else { TMPSTR = TmpStr Convert.TOCHAR (ABYTE);}}}}}} // reads a list of key in a section public void readsectionKeys (String SectionName) {keyArray.clear (); Byte [] Bytebuffer = New byte [11024 ]; // set Buffer length string tmpStr = null; if (GetPrivateProfileString (sectionName, null, null, byteBuffer, 1024, iniFileName)> 0) {foreach (byte abyte in byteBuffer) {if (abyte == 0) {if (tmpstr == null) {breaf;} else {keyArray.add (tmpstr); tmpstr = null;}} else {TM pStr = tmpStr Convert.ToChar (abyte);}}}} // Section determines whether or not the presence of public bool SectionExists (string sectionName) {if (sectionArray.Count <= 0) {ReadSections ();} foreach (object sectionList in sectionArray ) {if ((string) sectionList == sectionName) {return true;}} return false;} // determines whether the Key Section present public bool KeyExists (string sectionName, string keyName) {if (sectionArray.Count <= 0) {Readsections ();} foreach (object sectionList in sectionArray) {if ((string) sectionsList ==

sectionName) {foreach (object keyList in keyArray) {if ((string) keyList == keyName) {return true;}}}} return false;} // Returns Section List public ArrayList GetSectionValues ​​() {if (sectionArray.Count < = 0) {readsections ();} return (arraylist) sectionarray.clone ();}

/ / Return to the list of key in the section public ArrayList getKeyValues ​​(STRING SectionName) {readsectionKeys (SectionName); Return (arraylist) keyarray.clone ();

// read the data string public string ReadString (string sectionName, string keyName, string defaultVal) {StringBuilder retStr = new StringBuilder (1024);! If (GetPrivateProfileString (sectionName, keyName, defaultVal, retStr, 1024, iniFileName) = 0 ) {RETURN RETSTR.TOSTRING ();} else {return defaultVal;}}

// write string data public bool WriteString (string sectionName, string keyName, string setVal) {if (WritePrivateProfileString (sectionName, keyName, setVal, iniFileName) = 0!) {Return true;} else {return false;}}

// Read Digital Data Public Int Readinteger (String SectionName, String Keyname, Int DefaultVal) {Return Convert.Toint32 (SectionName, Keyname, Convert.TOSTRING (DefaultVal));}

// Write digital data public bool writeinteger (String SectionName, String Keyname, int setVal) {Return WritestRing (SectionName, Keyname, Convert.TOString (setVal));}

// Read BOOL Data Public Bool ReadBool (String Sectionname, String Keyname, Bool DefaultVal) {Return Convert.TOBOOLEAN (SectionName, Keyname, Convert.TOString (DefaultVal));}

// Write the BOOL Data Public Bool WriteBool (String SectionName, String Keyname, Bool SetVal) {Return WriteString (SectionName, Keyname, Convert.TOString (SETVAL));}

// Read Double Data public double ReadDouble (string sectionName, string keyName, double defaultVal) {return Convert.ToDouble (ReadString (sectionName, keyName, Convert.ToString (defaultVal)));} // public write data type Float Bool writedouble (String SectionName, String Keyname, Double SetVal) {Return WriteString (SectionName, Keyname, Convert.TOString (SETVAL));

// Read Data Datetime public DateTime ReadDatetime (string sectionName, string keyName, DateTime defaultVal) {return Convert.ToDateTime (ReadString (sectionName, keyName, Convert.ToString (defaultVal)));}

// Write a DateTime type data public bool write {return writString (sectionname, keyname, control);}

// Remove all section public bool deletesection (String SectionName) {if (SectionPrivateProfileString (SectionName, Null, Null, IniFileName)! = 0) {Return True;} else {Return False;}

}

// Section delete all the Key public bool DeleteKey (string sectionName, string KeyName) {if {return true (WritePrivateProfileString (sectionName, KeyName, null, iniFileName) = 0!);} Else {return false;}} // override Tostring Public Override string toString () {Return "this is inifile class";

Private string inifilename; private arraylist seatArray;}}

转载请注明原文地址:https://www.9cbs.com/read-101305.html

New Post(0)