C # reads the TXT configuration file and can update the configuration file

Source: Internet
Author: User

First, the configuration is delimited by the = sign, preceded by the expression of the configuration item, followed by the configured value

Function: Can read TXT configuration file and modify TXT configuration file

We can understand the form of key=value, above, can explain some, no nonsense, the code below it.

Private Static string_path_config = Application.startuppath +"\\config.txt";//configuration file        Private voidForm1_Load (Objectsender, EventArgs e) {            //reading configuration Fileslist<string[]> config =Readfromtxt (); if(Config! =NULL)            {                Try                {                    foreach(string[] Strinchconfig) {                        stringtemp = str[0]; Switch(temp. ToLower ()) { Case "Host": Txthost. Text= str[1];  Break;  Case "format": Txtgeshi. Text= str[1];  Break; }                    }                }                Catch { }            }        }        /// <summary>        ///Read txt/// </summary>        /// <param name= "path" >file path</param>        /// <returns>string Array</returns>        Privatelist<string[]>Readfromtxt () {Try            {                if(!System.IO.File.Exists (_path_config)) {                    return NULL; }                stringLine ; List<string[]> result =Newlist<string[]>(); StreamReader SR=NewStreamReader (_path_config);  while(line = Sr. ReadLine ())! =NULL) {result. ADD (line. Split ('=')); } Sr.                Close (); returnresult; }            Catch(Exception) {return NULL; }        }        /// <summary>        ///Update configuration file/// </summary>        /// <param name= "host" ></param>        /// <param name= "format" ></param>        Private voidUpdateconfig (stringHoststringformat) {            if(!string. IsNullOrEmpty (host) &&!string. IsNullOrEmpty (format)) {string[] line =New string[2]; line[0] ="host="+host; line[1] ="format="+format;            File.writealllines (_path_config, line); }} Copy Code

C # reads the TXT configuration file and can update the configuration file

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.