scene
Customize the xml configuration file in Winform and configure the access file path:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648
On the basis of the configuration and reading of the custom configuration file, we continue to read and write the configuration file.
The xml configuration file is as follows:
<?xml version="1.0" encoding="utf-8" ?> <Configure> <!--Y The default number of axes is 1--> <yConut>1</yConut> <!--Y Axis set--> <YAxis> <!--Article 1 Y axis--> <YAxi> <num>1</num> <title>temperature</title> <color>black</color> <min>-1500</min> <max>1500</max> </YAxi> <!--Second article Y axis--> <Yaxi> <num>2</num> <title>Voltage</title> <color>black</color> <min>-1500</min> <max>1500</max> </Yaxi> </YAxis> </Configure>
Focus on the Public Number
A hegemonic programmed ape
Get programming related e-books, tutorials and free downloads.
Mass Programming Video Tutorials: https://space.bilibili.com/164396311
Realization
Configuration file reading
Method to add a tool class
public static void readConfig() { //Get the path to the executable file-Namely bin Directory debug perhaps release Catalog string context = System.Windows.Forms.Application.StartupPath; string path = String.Concat(context,@"\config\YAxisSet.xml"); XmlDocument xml = new XmlDocument(); //Open one xml try { xml.Load(path); //Selection matching XPath The first expression XmlNode XmlNode Configure = xml.SelectSingleNode("Configure/YAxis/YAxi"); //Read node data if (Configure !=null) { string portName = Configure.SelectSingleNode("title").InnerText; MessageBox.Show("The first node name is:" + portName); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
Then add a button to call this method in the click event of the button.
private void simpleButton1_Click(object sender, EventArgs e) { ConfigAccessUtils.readConfig(); }
Effect
Write to configuration file
Similarly, new methods are added to the toolkit
public static void writeConfig() { //Get the path to the executable file string context = System.Windows.Forms.Application.StartupPath; string path = String.Concat(context, @"\config\YAxisSet.xml"); XmlDocument xml = new XmlDocument(); //Open one xml try { xml.Load(path); //Selection matching XPath The first expression XmlNode XmlNode Configure = xml.SelectSingleNode("Configure/YAxis/YAxi"); //Read node data if (Configure != null) { string portName = Configure.SelectSingleNode("title").InnerText; MessageBox.Show("Before writing, the node name is:" + portName); } //Write node data Configure.SelectSingleNode("title").InnerText = "Overbearing"; string afterWrite = Configure.SelectSingleNode("title").InnerText; xml.Save(path); MessageBox.Show("After writing, the node name is:" + afterWrite); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
Effect
Before writing
After writing
Note:
To modify the content of the configuration file, the real modification is the configuration file in the debug directory under bin.