Configuration file operations (INI, CFG, XML, config, etc.)

Source: Internet
Author: User
Tags connectionstrings

The format of the configuration file mainly includes INI, XML, config, etc., and now the operation of the configuration files in these formats (C #) is briefly explained.

INI configuration file operation

Call system Functions GetPrivateProfileString () and writeprivateprofilestring (), etc.

(1) Import library

[DllImport ("kernel32")]Private Static extern LongWritePrivateProfileString (stringSectionstringKeystringValstringFilePath); [DllImport ("kernel32")]Private Static extern intGetPrivateProfileString (stringSectionstringKeystringDef, StringBuilder RetVal,intSizestringFilePath);

(2) Calling function read/write INI configuration file

//ReadStringBuilder strcom =NewStringBuilder (255); GetPrivateProfileString ("Serial Port Parameters","Port","", Strcom,255,"Setting.ini");//WriteWritePrivateProfileString ("Serial Port Parameters","Port","COM3","Setting.ini");

CFG configuration file operation

configuration file Action component Sharpconfig is a. NET cfg/ini configuration file Action component.

Configuration file Example (SAMPLE.CFG):

[General]  == ten= 20.05= True

C # code example:

Configuration config = configuration.loadfromfile ("sample.cfg" ); Section Section= config[" General"];stringsomestring = section["somestring"]. Value;intSomeinteger = section["Someinteger"]. getvalue<int>();floatSomefloat = section["somefloat"]. getvalue<float> ();

The above Sharpconfig reference http://www.oschina.net/p/sharpconfig.

Config configuration file operation

The contents of the Appuser.config file are as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <appSettings>    <AddKey= "Svrname"value= "127.0.0.1"></Add>    <AddKey= "DBName"value= "Warehousedb"></Add>    <AddKey= "Usrname"value= "sa"></Add>    <AddKey= "PassWord"value= "sa"></Add>  </appSettings>  <!--Connection Database String -  <connectionStrings>    <Addname= "Dbconnstring"connectionString= "server=gaohongchen\sqlexpress;database=egdb;uid=sa;pwd=123456"/>  </connectionStrings></Configuration>

The core code of the C # dbconnstring reading the Appuser.config file is as follows:

New== = cfgfile.connectionstrings.connectionstrings["dbconnstring "]. ConnectionString;

For more information on the config configuration file, please refer to the following link:

    • Read/write App. Config File with. NET 2.0:http://www.codeproject.com/articles/14744/read-write-app-config-file-with-net-2-0
    • C # Read and write data in App. config: http://www.cnblogs.com/cadlife/articles/2245776.html
    • C # Read and write config configuration file: http://www.cnblogs.com/luxiaoxun/p/3599341.html

XML configuration file Operations

General operations for XML configuration files See my other blog post:

XML file Operations (C #): http://www.cnblogs.com/gaohongchen01/p/4788504.html

Configuration file operations (INI, CFG, XML, config, etc.)

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.