GetPrivateProfileString and writeprivateprofilestring operating ini configuration files in C # via C + +

Source: Internet
Author: User

Namespace Apimethod
{
/*
* Name: Tan Yi
* Time: 2008.5.28
* Q q:260511433
* MSN: [email protected]
* Email: [Email protected]
* Note: If you need to add or modify this module, please contact me, we work together.
* Note: This module can only get the value of a key, if who can perfect get all the keys and values of a bar, please contact me.
*/

<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>
Windows API to INI file Write method
</summary>
<param name= "Lpapplicationname" > The name of the subsection in which to write the new string. This string is case insensitive </param>
<param name= "Lpkeyname" > the item name or the entry name to be set. This string is not case-sensitive. Use NULL to delete all settings for this section </param>
<param name= "lpstring" > Specifies the string value to write for this entry. Delete the existing string of this item with NULL = </param>
<param name= "lpFileName" > Initialization file name. If you do not specify a full pathname, Windows looks 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>
Windows API to INI file Read method
</summary>
<param name= "Lpapplicationname" > The name of the subsection in which you want to find the entry. This string is not case-sensitive. If set to NULL, the list of all subsections of this INI file is loaded within the lpreturnedstring buffer </param>
<param name= "Lpkeyname" > the item Name or entry name you want to get. This string is not case-sensitive. If set to NULL, loads a list of all items in the specified section in the lpreturnedstring buffer </param>
<param name= "Lpdefault" > The specified entry is not found when the default value is returned. can be set to null ("") </param>
<param name= "lpreturnedstring" > Specify a string buffer with a length of at least nsize</param>
<param name= "NSize" > Specifies the maximum number of characters to load into the lpreturnedstring buffer </param>
<param name= "lpFileName" > Initialization file name. If you do not specify a full pathname, Windows looks for files in the Windows directory </param>
Note: If the lpkeyname parameter is null, then the lpreturnedstring buffer loads a list of all the settings in the specified subsection.
Each entry is separated by a null character, and the last item is aborted with two null characters. Please also refer to the annotations 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 values to the INI file
</summary>
<param name= "section" > The name of the subsection </param>
<param name= "key" > Key name </param>
<param name= "value" > key value </param>
<returns> execution succeeded to true and failed to false. </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);
}

       //<summary>
        ///delete the keys in the specified section
       //</summary
       //<param name= "section" > subsection name </param>
        //<param name= "key" > Key name </param>
        //<returns> execution succeeds to true, failure is false. </returns>
        public static long Deleteinikey (string section, String key)
        {
             if (section. Trim (). Length <= 0 | | Key. Trim (). Length <= 0) return 0;

return writeprivateprofilestring (section, key, NULL, FilePath);
}

<summary>
Deletes the specified subsection (including all keys in this section)
</summary>
<param name= "section" > The name of the subsection </param>
<returns> execution succeeded to true and failed to false. </returns>
public static long Deleteinisection (string section)
{
if (section. Trim (). Length <= 0) return 0;

return writeprivateprofilestring (section, NULL, NULL, FILEPATH);
}

<summary>
Gets the value of the key in the specified subsection
</summary>
<param name= "section" > The name of the subsection </param>
<param name= "key" > Key name </param>
<param name= "DefaultValue" > returns the specified default value if the key value is empty or not found. </param>
<param name= "Capacity" > Buffer initialization size. </param>
Values for <returns> keys </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 ();
}

       //<summary>
        ///Get the value of the key in the specified section
       //</summary
       //<param name= "section" > subsection name </param>
        //<param name= "key" > Key name </param>
        //<param name= "DefaultValue" > returns the specified default value if the key value is empty or not found. </param>
       ///<returns> value </returns>
         public static string Getinikeyvalue (string section, string key, String DefaultValue)
        {
             return Getinikeyvalue (section, key, DefaultValue, 1024);
       }

<summary>
Gets the value of the key in the specified subsection
</summary>
<param name= "section" > The name of the subsection </param>
<param name= "key" > Key name </param>
Values for <returns> keys </returns>
public static string Getinikeyvalue (string section, string key)
{
return Getinikeyvalue (section, key, String. Empty, 1024);
}
}
}

GetPrivateProfileString and writeprivateprofilestring operating ini configuration files in C # via C + +

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.