Windows API reads the ini configuration file. I learned it today and wrote it here for you to share. I hope you can make a lot of bricks.
Using System. Runtime. InteropServices;
Using System. Text;
Namespace CMP_WX
{
/**/
/// <Summary>
/// Class for reading and writing INI files
/// Call two APIs in kernel32.dll: WritePrivateProfileString and GetPrivateProfileString
/// To read and write ini files.
/// The INI file is a text file,
/// Consists of several sections,
/// Under each title with parentheses,
/// Several keywords and their corresponding values)
/// Example:
/// [Section]
/// Key = value
/// </Summary>
Public class Read_INT
{
/**/
/// <Summary>
/// INI File Name (with Path)
/// </Summary>
Public string filePath;
/**/
/// Declare the API function for reading and writing INI files
[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 );
/**/
/// <Summary>
/// Constructor of the class
/// </Summary>
/// <Param name = "INIPath"> INI file name </param>
Public Read_INT (string INIPath)
{
FilePath = INIPath;
}
/**/
/// <Summary>
/// Write the INI File
/// </Summary>
/// <Param name = "Section"> Section </param>
/// <Param name = "Key"> Key </param>
/// <Param name = "value"> value </param>
Public void WriteInivalue (string Section, string Key, string value)
{
WritePrivateProfileString (Section, Key, value, this. filePath );
}
/**/
/// <Summary>
/// Read the specified part of the INI File
/// </Summary>
/// <Param name = "Section"> Section </param>
/// <Param name = "Key"> Key </param>
/// <Returns> String </returns>
Public string ReadInivalue (string Section, string Key)
{
StringBuilder temp = new StringBuilder (1024 );
Int I = GetPrivateProfileString (Section, Key, "read error", temp, 1024, this. filePath );
Return temp. ToString ();
}
}
}