When developing a Web project, a configuration file, Web. config, is used to hold global variables, such as strings used to connect to the database. Accordingly, in the development of the WinForm program, there is also a configuration file, which is the app. config, which functions roughly the same as Web. config and can be used to store the global variables and value values used by the program.
Look at an example of an app. Config file:
<?XML version= "1.0" encoding= "Utf-8"?>
<Configuration>
<AppSettings>
picture storage path -->
<add key= " Imgpath " value=" D:\img\ " />
</appsettings >
</configuration>
As you can see, app. Config and Web. config, well, it's also an XML file. So how do you read the elements in this file? Very simple, look at the code:
string strpath = system.configuration.configurationsettings.appsettings["imgpath" ]. ToString ();
This allows you to read the value of the Imgpath element in the App. config file. So how do you rewrite the value of the element? If you think to write like that read, code like this:
system.configuration.configurationsettings.appsettings["imgpath"] = @ "E:\img \"; // It's useless to write like this
When modifying the element value of the app. Config file, you can only treat the app. config file as a normal Xml file, use the System.Xml.XmlDocument class to read the app. config file into memory, and pass the System.Xml . The XmlNode class finds the appsettings node, After the System.Xml.XmlElement class finds an element under the node and uses the SetAttribute method to modify the value of the element, it finally saves the App. config file to the original directory so that the modification of the value of an element is completed. The following method can be done to modify any element under the appsettings node of the App. Config file, and of course, you may modify this method to change the value of any element in any node.
The WinForm program reads and overwrites the value of the config file app.