[Dllimport (kernel32.dll)]
The dynamic link library kernel32.dll is introduced, which contains many Windows API functions, which are often used to read and write INI files through windows APIs, the following is a piece of detailed information from others.
Namespace apimethod
{
/// <Summary>
/// INI file operation class
/// </Summary>
Public sealed class iniutility
{
Private Static string _ filepath = string. Empty; // file path
/// <Summary>
/// File path
/// </Summary>
Public static string filepath
{
Get {return _ filepath ;}
Set {_ filepath = value ;}
}
/// <Summary>
/// How to write the INI file using Windows API
/// </Summary>
/// <Param name = "lpapplicationname"> name of the section in which you want to write a new string. This string is case insensitive </param>
/// <Param name = "lpkeyname"> name of the item or entry to be set. This string is case insensitive. Use null to delete all settings in this section </param>
/// <Param name = "lpstring"> specify the string value written for this item. Use null to delete the existing string of this item </param>
/// <Param name = "lpfilename"> name of the initialization file. If the complete path name is not specified, Windows searches for files in the Windows directory. If the file is not found, the function creates it. </param>
/// <Returns> </returns>
[System. runtime. interopservices. dllimport ("Kernel32")]
Private Static extern long writeprivateprofilestring (string lpapplicationname, string lpkeyname, string lpstring, string lpfilename );
/// <Summary>
/// How does Windows API read INI files?
/// </Summary>
/// <Param name = "lpapplicationname"> name of the section in which you want to find entries. This string is case insensitive. If it is set to null, the list of all sections of the INI file will be loaded in the lpreturnedstring buffer. </param>
/// <Param name = "lpkeyname"> name of the item to be obtained. This string is case insensitive. If this parameter is set to null, all items in the specified section are loaded in the lpreturnedstring buffer. </param>
/// <Param name = "lpdefault"> the default value returned when the specified entry is not found. Can be set to null ("") </param>
/// <Param name = "lpreturnedstring"> specify a string buffer with a length of at least nsize. </param>
/// <Param name = "nsize"> specify the maximum number of characters loaded to the lpreturnedstring buffer </param>
/// <Param name = "lpfilename"> name of the initialization file. If no full path name is specified, search for the file in the Windows directory. </param>
/// Note: If the lpkeyname parameter is null, The lpreturnedstring buffer will load a list of all the settings in the specified section.
/// Each item is separated by a null character, and the last item is aborted with two null characters. For more information, see the annotation of the getprivateprofileint function.
/// <Returns> </returns>
[System. runtime. interopservices. dllimport ("Kernel32")]
Private Static extern long getprivateprofilestring (string lpapplicationname, string lpkeyname, string lpdefault, system. Text. stringbuilder lpreturnedstring, int nsize, string lpfilename );
/// <Summary>
/// Write the value to the INI File
/// </Summary>
/// <Param name = "section"> name of a section </param>
/// <Param name = "key"> key name </param>
/// <Param name = "value"> key value </param>
/// <Returns> If the execution succeeds, the value true indicates that the execution fails. If the execution fails, the value false indicates that the execution succeeds. </Returns>
Public static long writeinikey (string section, string key, string value)
{
If (section. Trim (). Length <= 0 | key. Trim (). Length <= 0 |
Value. Trim (). Length <= 0) return 0;
Return writeprivateprofilestring (section, key, value, filepath );
}
///
/// Delete the key in the specified section
///
/// section name
/// key name
/// true if execution is successful, failure is false.
Public static long deleteinikey (string section, string key)
{< br> If (section. trim (). length <= 0 | key. trim (). length <= 0) return 0;
Return writeprivateprofilestring (section, key, null, filepath );
}
/// <Summary>
/// Delete the specified section (including all the keys in this section)
/// </Summary>
/// <Param name = "section"> name of a section </param>
/// <Returns> If the execution succeeds, the value true indicates that the execution fails. If the execution fails, the value false indicates that the execution succeeds. </Returns>
Public static long deleteinisection (string section)
{
If (section. Trim (). Length <= 0) return 0;
Return writeprivateprofilestring (section, null, null, filepath );
}
/// <Summary>
/// Obtain the key value of the specified section
/// </Summary>
/// <Param name = "section"> name of a section </param>
/// <Param name = "key"> key name </param>
/// <Param name = "defaultvalue"> If the key value is null or cannot be found, the specified default value is returned. </Param>
/// <Param name = "capacity"> buffer initialization size. </Param>
/// <Returns> key value </returns>
Public static string getinikeyvalue (string section, string key, string defaultvalue, int capacity)
{
If (section. Trim (). Length <= 0 | key. Trim (). Length <= 0) return defaultvalue;
System. Text. stringbuilder strtemp = new system. Text. stringbuilder (capacity );
Long returnvalue = getprivateprofilestring (section, key, defaultvalue, strtemp, capacity, filepath );
Return strtemp. tostring (). Trim ();
}
///
/// obtain the key value of the specified section.
///
/// section name
// key name
// If the key value is null, the specified default value is returned.
// key value
Public static string getinikeyvalue (string section, string key, string defaultvalue)
{< br> return getinikeyvalue (section, key, defaultvalue, 1024);
}
///
/// obtain the key value of the specified section.
///
/// section name
// key name
// key value
Public static string getinikeyvalue (string section, string key)
{< br> return getinikeyvalue (section, key, String. empty, 1024);
}< BR >}
Source Address: http://kb.cnblogs.com/a/1209300/