Use a C # operation ini file
Original: Blazinix Translation: DragonTt
This class, encapsulates the methods provided in kernal32.dll to operate the INI file.
Introduction:
A class is created here, encapsulating two methods provided in kernel32.dll, used to operate the INI file. These two methods are: WritePrivateProfileString and GetPrivateProfileString.
The namespaces that need references are: system.Runtime.InterOpServices and System.Text
Class source file
Using system;
Using system.Runtime.InteropServices;
Using system.text;
Namespace Ini
{
///
/// Create a new ini file to store or loading data
/// summary>
Public class inIfile
{
PUBLIC STRING PATH;
[DLLIMPORT ("kernel32")]]
Private static extern long writPrivateProfileString (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;
///
/// inIfile constructor.
/// summary>
/// param>
Public inIfile (String InIPATH)
{
Path = inipath;
}
///
/// write data to the in iNi file
/// summary>
/// param>
/// section name
/// param>
/// Key Name
/// param>
/// Value Name
Public void IniwriteValue (String Section, String Key, String Value)
{
WritePrivateProfileString (Section, Key, Value, this.path);
}
///
/// read data value from the ini file
/// summary>
/// param>
/// param>
/// param>
///
{
StringBuilder Temp = New StringBuilder (255);
INT i = getPrivateProfileString (Section, Key, ", TEMP,
255, this.path);
Return Temp.toString ();
}
}
}
Use this class
Follow these steps:
1. Add a reference to namespaces in your project
Using ini;
2. Create a following INIFILE object
INIFILE INI = New InIfile ("C: //Test.ini");
3. Use the INIWRITEVALUE method to give a key payment value in the specified configuration, or use the InireAdValue method to read the value of a key in the specified configuration section.
As mentioned above, it is very easy to encapsulate the API function into your class in C #.