SoFunction
Updated on 2025-03-07

C# How to get configuration file contents

This article describes how to obtain configuration file content in C#. Share it for your reference. The specific implementation method is as follows:

Provides access to client application configuration files.

It has two properties: ConnectionStrings Gets the ConnectionStringsSection data configured by the default of the current application.

Method 1:

Copy the codeThe code is as follows:
string myConn =["sqlConnectionString"].ConnectionString;

Method 2:

Copy the codeThe code is as follows:
string connString =["sqlConnectionString"].ToString();

The configuration in the following is:

Copy the codeThe code is as follows:
<configuration>
<connectionStrings>
    <add name="sqlConnectionString" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
      providerName="" />
</connectionStrings>
</configuration>

Gets the AppSettingsSection data configured by the current application by default.

Copy the codeThe code is as follows:
string myConn = ["sqlConnectionString"].ToString();

The configuration in the following is:

Copy the codeThe code is as follows:
<configuration>
<appSettings>
    <add key="sqlConnectionString" value="Monday, January 23, 2006 2:56:14 PM" />
</appSettings>
</configuration>

I hope this article will be helpful to everyone's C# programming.