Reading Settings from App.Config or Web.Config in .Net

Reading settings from app.config or web.config in .NET

You'll need to add a reference to System.Configuration in your project's references folder.

You should definitely be using the ConfigurationManager over the obsolete ConfigurationSettings.

How to read web.config file in .Net Core app

Because .Net Core applications are self hosted and can almost run on any platform, they are no longer hosted on IIS. The .Net Core application settings are stored in a Json format (appsettings.json) by default while .Net Framework application configurations are stored in a web.config file in XML format. For more info about .Net Core applications, you may read Configuration in ASP.NET Core. In my case, I was trying to access the data layer of a .Net Framework assembly from a .Net Core 2.0 assembly. To achieve this, there is no need to install System.Configuration.ConfigurationManager package in the .Net Core application but you only need to add app.config to the .Net Core assembly then add the connection string to it:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="SHOPPINGCNN" connectionString="server=your server name;integrated security=true;database=your database name" />
</connectionStrings>
</configuration>

After that, everything will work fine. Make sure that you use the same connection string name (SHOPPINGCNN in my case) that you used in your .Net Framework application otherwise you will not get the desired result. I did this in my project and it works 100%.

C#/ASP.Net: app.settings instead of web.config

Hi I found the solution:

if yu want to get the properties of app.config you can use:

var value = Properties.Settings.Default.keyName

Reading settings from app.config in .net 4.5

Make sure the (app name here).config file actually appears in the same folder as your (app name here).exe file. Being that you called it App1.config, I'm guessing that you have more than one.

Visual Studio renames App.Config to the actual (app name here).config file during a build, not App1.config.

Reading a key from the Web.Config using ConfigurationManager

Try using the WebConfigurationManager class from the System.Web.Configuration namespace instead. For example:

string userName = WebConfigurationManager.AppSettings["PFUserName"]

What is the difference between App.Config and Web.Config in .Net?

The choice of the configuration file name is determined by the hosting environment you choose for the service. If you are using IIS to host your service, use a Web.config file. If you are using any other hosting environment, use an App.config file.

In Visual Studio, the file named App.config is used to create the final configuration file. The final name actually used for the configuration depends on the assembly name. For example, an assembly named "Cohowinery.exe" has a final configuration file name of "Cohowinery.exe.config". However, you only need to modify the App.config file. Changes made to that file are automatically made to the final application configuration file at compile time.

In using an App.config, file the configuration system merges the App.config file with content of the Machine.config file when the application starts and the configuration is applied. This mechanism allows machine-wide settings to be defined in the Machine.config file. The App.config file can be used to override the settings of the Machine.config file; you can also lock in the settings in Machine.config file so that they get used. In the Web.config case, the configuration system merges the Web.config files in all directories leading up to the application directory into the configuration that gets applied.

App.config v.s. Web.config: which has priority?

From MSDN for .NET 4.5:

When configuring a service in Visual Studio, use either a Web.config file or an App.config file to specify the settings. The choice of the configuration file name is determined by the hosting environment you choose for the service. If you are using IIS to host your service, use a Web.config file. If you are using any other hosting environment, use an App.config file.

In Visual Studio, the file named App.config is used to create the final configuration file. The final name actually used for the configuration depends on the assembly name. For example, an assembly named "Cohowinery.exe" has a final configuration file name of "Cohowinery.exe.config". However, you only need to modify the App.config file. Changes made to that file are automatically made to the final application configuration file at compile time.

In using an App.config, file the configuration system merges the App.config file with content of the Machine.config file when the application starts and the configuration is applied. This mechanism allows machine-wide settings to be defined in the Machine.config file. The App.config file can be used to override the settings of the Machine.config file; you can also lock in the settings in Machine.config file so that they get used. In the Web.config case, the configuration system merges the Web.config files in all directories leading up to the application directory into the configuration that gets applied. For more information about configuration and the setting priorities, see topics in the System.Configuration namespace.

Here's also a great post for those using MS Azure, explaining the differences b/w ApplicationSettings, appSettings (app.config/web.config) and ConfigurationSettings (.csdef / .cscfg):

http://haishibai.blogspot.com/2012/09/windows-azure-cloud-service.html

Struggling to read value from web.config file using ConfigurationManager.AppSettings[ key ] in ASP .NET

Rename the web.config file to app.config and then it should work



Related Topics



Leave a reply



Submit