The WinForm program reads and overwrites the value of the config file app.

Source: Internet
Author: User

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.

Related Article

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.