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