C # How to read ini files

Source: Internet
Author: User

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 ();
}
}
}

Related Article

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.