SoFunction
Updated on 2025-03-07

Read configuration file code instance in C#

It is the configuration file for developing WinForm programs in C#, and the configuration file for developing Web programs is called. Introduction to the introduction of this article.

Let's first open a file and see what its content looks like.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <connectionStrings>
  <add name="conn" connectionString="this is connection string"/>
 </connectionStrings>
 <appSettings>
  <add key="key1" value="value1" />
  <add key="key2" value="value2" />
 </appSettings>
</configuration>

From this code, it can be seen that it is completely an XML document. It has the following characteristics.
1. It has a <configuration> tag, and all configuration items are under the <configuration> tag.
2. C# has a built-in <connectionStrings> node, which is specifically used to configure database connection strings. It can use the <Add> node to add multiple database connection strings below.
3. <appSettings>, everyone should be familiar with it. It can configure any key-value pair like key-value. When there was no <connectionStrings> at the beginning, we also placed the database connection string in <appSettings>.

Let’s write a program to see how to obtain configuration information from this program configuration file.

using System;
using ;
using ;
using ;
using ;
using ;

namespace AppConfig
{
  class Program
  {
    static void Main(string[] args)
    {
      ("ConnectionStrings:");
      // is a ConnectionStringSettingsCollection object      // Loop by number to get ConnectionStringSettings objects      // Each ConnectionStringSettings object has Name and ConnectionString properties      for (int i = 0; i &lt; ; i++)
      {
        string name = [i].Name;
        string connectionString = [i].ConnectionString;
        (() + ". " + name + " = " + connectionString);
      }
      //You can also use the ConnectionStringSettings type to perform foreach traversal      foreach (ConnectionStringSettings conn in )
      {
        string name = ;
        string connectionString = ;
        (name + " = " + connectionString);
      }
      //Turnly get the value of conn      ("\r\nGet the value of the node named \"conn\":");
      (["conn"].ConnectionString);
      ("");

      ("AppSettings:");
      //AppSettings is the NameValueConnection type, and uses AllKeys to return an array of strings composed of all keys.      string[] keys = ;
      for (int i = 0; i &lt; ; i++)
      {
        string key = keys[i];
        //Index Value through Key        string value = [key];
        (() + ". " + key + " = " + value);
      }
      // There is no object like NameValuePair, so it is impossible to use foreach to loop
      //Turnly get the value of key1      ("\r\nGet the value of the key named \"key1\":");
      (["key1"]);

      //pause the process
      ();
    }
  }
}

The above demonstrates how to traverse <connectionStrings>, how to traverse <appSettings>, how to get a connectionString separately, and how to get a certain appSetting separately. You can bookmark it for reference when using it later.

The syntax format is exactly the same, just a user WinForm program and one is used for web programs.

After compilation, it will be compiled into the bin\Debug directory, and the file name is "Application Name."
 
This article introduces so much about the use of files in C#. I hope it will be helpful to you. Thank you!