Because I often need to operate the INI file and publish the INI class I used.
It was previously found on the network and has full functionality.
Public class INIFILE
{
Public String filename; // INI File Name
// Declare the API function for reading and writing INI files
[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, passing the INI File Name
///
Public INIFILE (string afilename)
{
// Determine whether a file exists
Fileinfo = new fileinfo (afilename );
// Todo: Find out the usage of enumeration
If ((! Fileinfo. exists ))
Throw (New applicationexception ("INI file does not exist "));
Filename = fileinfo. fullname;
}
// Write the INI File
/// <Summary>
/// Write the INI File
/// </Summary>
/// <Param name = "section"> </param>
/// <Param name = "ident"> </param>
/// <Param name = "value"> </param>
Public void iniwritevalue (string section, string Ident, string value)
{
If (! Writeprivateprofilestring (section, ident, value, filename ))
{
// Todo: throw a custom exception.
Throw (New applicationexception ("An error occurred while writing the INI file "));
}
}
// Read the specified INI File
/// <Summary>
/// Read the specified INI File
/// </Summary>
/// <Param name = "section"> </param>
/// <Param name = "ident"> </param>
/// <Returns> </returns>
Public String inireadvalue (string section, string ident)
{
// Stringbuilder buffer = new stringbuilder (255 );
Byte [] buffer = new byte [65535];
Int buflen = getprivateprofilestring (section, ident, "", buffer, buffer. getupperbound (0), filename );
// The value 0 must be set. Code Page). Otherwise, Chinese characters cannot be supported.
String S = encoding. getencoding (0). getstring (buffer );
S = S. substring (0, buflen );
Return S. Trim ();
}
// Read integer
/// <Summary>
/// Read integer
/// </Summary>
/// <Param name = "section"> </param>
/// <Param name = "ident"> </param>
/// <Param name = "default"> </param>
/// <Returns> </returns>
Public int readinteger (string section, string Ident, int default)
{
String intstr = inireadvalue (section, ident );
Try
{
Return convert. toint32 (intstr );
}
Catch (exception ex)
{
Console. writeline (ex. Message );
Return default;
}
}
// Write an integer
/// <Summary>
/// Write an integer
/// </Summary>
/// <Param name = "section"> </param>
/// <Param name = "ident"> </param>
/// <Param name = "value"> </param>
Public void writeinteger (string section, string Ident, int value)
{
Iniwritevalue (section, ident, value. tostring ());
}
// Read Boolean
/// <Summary>
/// Read Boolean
/// </Summary>
/// <Param name = "section"> </param>
/// <Param name = "ident"> </param>
/// <Param name = "default"> </param>
/// <Returns> </returns>
Public bool readbool (string section, string Ident, bool default)
{
Try
{
Return convert. toboolean (inireadvalue (section, ident ));
}
Catch (exception ex)
{
Console. writeline (ex. Message );
Return default;
}
}
// Write bool
/// <Summary>
/// Write bool
/// </Summary>
/// <Param name = "section"> </param>
/// <Param name = "ident"> </param>
/// <Param name = "value"> </param>
Public void writebool (string section, string Ident, bool value)
{
Iniwritevalue (section, ident, convert. tostring (value ));
}
// From the INI file, add all ident in the specified section name to the list
/// <Summary>
/// From the INI file, add all ident in the specified section name to the list
/// </Summary>
/// <Param name = "section"> </param>
/// <Param name = "idents"> </param>
Public void readsection (string section, stringcollection idents)
{
Byte [] buffer = new byte [16384];
// Idents. Clear ();
Int buflen = getprivateprofilestring (section, null, null, buffer, buffer. getupperbound (0 ),
Filename );
// Parse the Section
Getstringsfrombuffer (buffer, buflen, idents );
}
Private void getstringsfrombuffer (byte [] buffer, int buflen, stringcollection strings)
{
Strings. 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;
}
}
}
}
// Read the names of all the sections s from the INI File
/// <Summary>
/// Read the names of all the sections s from the INI File
/// </Summary>
/// <Param name = "sectionlist"> </param>
Public void readsections (stringcollection sectionlist)
{
// Note: bytes must be used for implementation. stringbuilder can only obtain the first section.
Byte [] buffer = new byte [65535];
Int buflen = 0;
Buflen = getprivateprofilestring (null, buffer,
Buffer. getupperbound (0), filename );
Getstringsfrombuffer (buffer, buflen, sectionlist );
}
// Read all values of the specified section to the list
/// <Summary>
/// Read all values of the specified section to the list
/// </Summary>
/// <Param name = "section"> </param>
/// <Param name = "values"> </param>
Public void readsectionvalues (string section, namevaluecollection values)
{
Stringcollection keylist = new stringcollection ();
Readsection (section, keylist );
Values. Clear ();
Foreach (string key in keylist)
{
Values. Add (Key, inireadvalue (section, key ));
}
}
// Clear a Section
/// <Summary>
/// Clear a Section
/// </Summary>
/// <Param name = "section"> </param>
Public void erasesection (string section)
{
//
If (! Writeprivateprofilestring (section, null, null, filename ))
{
Throw (New applicationexception ("section in the INI file cannot be cleared "));
}
}
// Delete the key under a section
/// <Summary>
/// Delete the key under a section
/// </Summary>
/// <Param name = "section"> </param>
/// <Param name = "ident"> </param>
Public void deletekey (string section, string ident)
{
Writeprivateprofilestring (section, ident, null, filename );
}
// Note: For Win9x, You need to implement the updatefile method to write data in the buffer to the file.
// On Win NT, 2000, and XP, files are directly written without buffering. Therefore, you do not need to implement updatefile.
// After modifying the INI file, call this method to update the buffer zone.
Public void updatefile ()
{
Writeprivateprofilestring (null, filename );
}
// Check whether a key value in a section exists
Public bool valueexists (string section, string ident)
{
//
Stringcollection idents = new stringcollection ();
Readsection (section, idents );
Return idents. indexof (ident)>-1;
}
// Ensure resource release
~ INIFILE ()
{
Updatefile ();
}
}