*. Ini file and writeprivateprofilestring () and getprivateprofilestring ()

Source: Internet
Author: User

INI is a file with the extension "ini". This file mainly stores users' selections and various system parameters. You can modify the INI file to change many configurations of applications and systems.

C # The namespace does not directly read and write ini classes. Of course, if you use the int type as a text file and use the system. Io class to read and write data, you can directly use break.

In the "kernel32.dll" file, Win32 API functions-writeprivateprofilestring () and getprivateprofilestring () can be used to read and write *. ini files.

Declare the write operation function writeprivateprofilestring ():
[Dllimport ("Kernel32")]

/// Section: Section in the INI File

/// Key: keyword in the INI File

/// VAL: value of the keyword in the INI File

/// Filepath: the complete path and name of the INI File
Private Static extern long writeprivateprofilestring (string section, string key, string Val, string filepath)

Declare the read operation function getprivateprofilestring ():

[Dllimport ("Kernel32")]

/// Section: section name in the INI File

/// Key: keyword in the INI File

/// Def: default value when cannot be read

/// Retval: Read the value

/// Size: value size

/// Filepath: the complete path and name of the INI File
Private Static extern int getprivateprofilestring (string section, string key, string def, stringbuilder retval, int size, string filepath)

 

 

The operation class of *. ini file is as follows:

Public class iniclass
{
Public String inipath;
[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>
/// </Summary>
/// <Param name = "inipath"> file path </param>
Public iniclass (string inipath)
{
Inipath = inipath;
}
/// <Summary>
/// Write the INI File
/// </Summary>
/// <Param name = "section"> paragraph name </param>
/// <Param name = "key"> keyword </param>
/// <Param name = "value"> value of the keyword </param>
Public void iniwritevalue (string section, string key, string value)
{
Writeprivateprofilestring (section, key, value, this. inipath );
}
/// <Summary>
/// Read the INI File
/// </Summary>
/// <Param name = "section"> paragraph name </param>
/// <Param name = "key"> keyword </param>
Public String inireadvalue (string section, string key)
{
Stringbuilder temp = new stringbuilder (500 );
Int I = getprivateprofilestring (section, key, "", temp, 500, this. inipath );
Return temp. tostring ();
}
/// <Summary>
/// Verify whether the file exists
/// </Summary>
Public bool existinifile ()
{
Return file. exists (inipath );
}
}
 

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.