Read and write INI configuration files in C # (GO)

Source: Internet
Author: User

Management configuration is essential when developing application systems. such as the configuration of the database server, installation and update configuration, and so on. Due to the rise of XML, most of the current configuration files are stored as XML documents. For example, the configuration file Web. config for Visual Studio.NET itself mashine.config,asp.net, including the configuration files that I mentioned in the introduction remoting, are in XML format.

The traditional configuration file ini has been gradually replaced by the XML file trend, but for a simple configuration, INI file is still useful. INI file is actually a text file, it has a fixed format, section of the name is enclosed in [], and then wrapped to explain the value of key:
[Section]
Key=value

such as database server configuration file:

Dbserver.ini

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

In C #, the reading and writing of configuration files is done through API functions, and the code is simple:

Using system;using system.text;using system.io;using system.runtime.interopservices;namespace PubOp{public class Oper Ateinifile {#region API function declaration [DllImport ("kernel32")]//returns 0 for failure, not 0 for success private static extern long Wri        Teprivateprofilestring (String section,string Key, String val,string FilePath); [DllImport ("kernel32")]//returns the length of the string buffer obtained by the private static extern long GetPrivateProfileString (string section,string key        , String Def,stringbuilder retval,int size,string filePath); #endregion #region Read INI file public static string Readinidata (String section,string key,string notext,string in Ifilepath) {if (file.exists (IniFilePath)) {StringBuilder temp = new Stringbui                Lder (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 Opstat                    Ion = WritePrivateProfileString (Section,key,value,inifilepath);                if (opstation = = 0) {return false;                } else {return true;            }} else {return false; }} #endregion}}


Briefly describe the following methods for Writeinidata () and Readinidata () parameters.

The section parameter, the key parameter, and the IniFilePath do not need to say, the value parameter indicates the key, and here the Notext corresponds to the DEF parameter of the API function, and its value is specified by the user, when no specific value is found in the configuration file. Replace it with the value of Notext.

Notext can be null or ""

Original address: http://www.cnblogs.com/wayfarer/archive/2004/07/16/24783.html

Read and write INI configuration files in C # (GO)

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.