INI file read/write (from API perspective)

Source: Internet
Author: User

Before writing this article, I searched for blog posts about ini reading and writing on the Internet, but it is difficult to find the method for reading all the keys in the Section (finally I found ^_^ ). Next, let's talk about ini :( ** I am a CP **)

 

** Management configuration is essential for application system development. For example, configure, install, and update the database server. Due to the rise of XML, most configuration files are stored as XML files. For example, Visual Studio. NET's own configuration file mashine. config, ASP. NET's configuration file web. config, including the configuration files I mentioned in remoting, are all in XML format.

The traditional configuration file INI has been gradually replaced by XML files, but for simple configuration, the INI file is still useful. The INI file is actually a text file, which has a fixed format. The section name is enclosed in [], and the key value is displayed in line feed:
[Section]
Key = Value

For example, the database server configuration file:

Dbserver. ini

[Server]
Name = localhost
[DB]
Name = northwind
[User]
Name = sa **

 

In C #, INI can be operated only through win api, so we need to see how the API is written:

(1) bool writeprivateprofilestring (
Lptstr lpappname,
Lptstr lpkeyname,
Lptstr lpstring,
Lptstr lpfilename
);
Meanings of parameters:
Lptstr lpappname is a field name in the INI file.
Lptstr lpkeyname is a key name under lpappname. Generally, it is a variable name.
Lpstring is the key value, that is, the value of the variable. However, the value must be of the lpctstr or cstring type.

Lptstr lpfilename is the complete INI file name.
(2) DWORD getprivateprofilestring (
Lptstr lpappname,
Lptstr lpkeyname,
Lptstr lpdefault,
Lptstr lpreturnedstring,
DWORD nsize,
Lptstr lpfilename
);
Meanings of parameters:
The first two parameters have the same meaning as writeprivateprofilestring.

Lpdefault: If the INI file does not contain the field name or key name specified by the first two parameters, this value is assigned to the variable.
Lpreturnedstring: The cstring object that receives the value in the INI file, that is, the destination cache.
Nsize: the size of the destination cache.
Lpfilename: the complete INI file name.
Most of the API parameters of all versions on the Internet are consistent. Except for the lptstr lpreturnedstring 4th parameter of getprivateprofilestring, The cstring object that receives the values in the INI file, that is, the destination cache. Since it is a cache, it may be an array or stringbuilder. therefore, we have two options.

When selecting the first type, we can take all the keys of the Section into the lpreturnedstring.

When selecting the first type, we can only take the first key of the Section into the lpreturnedstring.

I think everyone understands it. Check the Code:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;
Using system. runtime. interopservices;

Namespace inireadandwrite
{
Class Program
{
# Region API function declaration

[Dllimport ("Kernel32")]
Private Static extern long writeprivateprofilestring (string section, string key,
String Val, string filepath );

[Dllimport ("Kernel32")]
// [Difficulty 1] 4th parameters can also be written as stringbuilder retval; set it to byte [] to support difficulty 2
Private Static extern long getprivateprofilestring (string section, string key,
String def, byte [] retval, int size, string filepath );

# Endregion
# Region read INI files
// [Difficulty 2] when the key is null, all the keys under the section can be obtained.
Public static string readinidata (string section, string key, string notext, string inifilepath)
{
If (file. exists (inifilepath ))
{
Byte [] temp = new byte [1, 65535];
Long buflen = getprivateprofilestring (section, key, notext, temp, temp. getupperbound (0), inifilepath );
String S = encoding. getencoding (0). getstring (temp );
S = S. substring (0, (INT) buflen );
Return S. Trim ();
// The following 4th parameters are stringbuilder
// Stringbuilder temp = new stringbuilder (1024 );
// Getprivateprofilestring (section, key, notext, temp, 1024, inifilepath );
// Return temp. tostring ();
}
Else
{
Return string. empty;
}
}

# Endregion

# Region write INI File
Public static bool writeinidata (string section, string key, string value, string inifilepath)
{
If (file. exists (inifilepath ))
{
Long opstation = writeprivateprofilestring (section, key, value, inifilepath );
If (opstation = 0)
{
Return false;
}
Else
{
Return true;
}
}
Else
{
Return false;
}
}
# Endregion
Static void main (string [] ARGs)
{
Writeinidata ("staus", "vbts5", "running", @ "C: // T. ini ");
Writeinidata ("staus", "vbts4", "running", @ "C: // T. ini ");
Writeinidata ("staus", "vbts5", "notrunning", @ "C: // T. ini ");

Console. writeline (readinidata ("staus", "vbts5", "", @ "C: // T. ini "));
Console. writeline (readinidata ("staus", null, null, @ "C: // T. ini "));
}
}
}

BTW: You can leave a message to indicate my error. Thank you.

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.