SoFunction
Updated on 2025-03-07

(C#) Application configuration file/add, delete, and modify operations

The configuration file is the basis and basis for the program itself. Its essence is an XML file. It is very convenient for the operation of the configuration file starting from .NET 2.0. It provides the NameSpace of the management function System [.Web] .Configuration. To use it, you need to add a reference to it.
For WINFORM programs, use ;
For programs, use;
It is really common for reading configuration file content. If you do not read configuration file content in your program, you are embarrassed to use it.
Let's take the most common AppSettings section as an example:
Suppose there is the following configuration file content:
Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="y" value="this is Y"/>
</appSettings>
</configuration>

1. Read the value:
* : [“y”];
* WinForm: [“y”];
2. Add an item
(Requires write permission):
Configuration config = (null);
AppSettingsSection app = ;
("x", "this is X");
();
WinForm:
Configuration config = ();
AppSettingsSection app = ;
("x", "this is X");
();
3. Modify one
*
Configuration config = (null);
AppSettingsSection app = ;
//("x", "this is X");
["x"].Value = "this is not Y";
();
* WinForm
Configuration config = ();
AppSettingsSection app = ;
//("x", "this is X");
["x"].Value = "this is not Y";
();
4. Delete an item
*
Configuration config = (null);
AppSettingsSection app = ;
("x");
();
* WinForm
Configuration config = ();
AppSettingsSection app = ;
("x");
();