223

I have done a project in C#.NET where my database file is an Excel workbook. Since the location of the connection string is hard coded in my coding, there is no problem for installing it in my system, but for other systems there is.

Is there a way to prompt the user to set a path once after the setup of the application is completed?

The answers I got was "Use App.Config"... can anyone tell what is this App.config and how to use it in my context here?

bluish
  • 26,356
  • 27
  • 122
  • 180
shyam
  • 2,259
  • 2
  • 13
  • 5
  • 5
    possible duplicate of [What is app.config for?](http://stackoverflow.com/questions/1431742/what-is-app-config-for) – user Dec 04 '13 at 07:01

7 Answers7

237

At its simplest, the app.config is an XML file with many predefined configuration sections available and support for custom configuration sections. A "configuration section" is a snippet of XML with a schema meant to store some type of information.

Settings can be configured using built-in configuration sections such as connectionStrings or appSettings. You can add your own custom configuration sections; this is an advanced topic, but very powerful for building strongly-typed configuration files.

Web applications typically have a web.config, while Windows GUI/service applications have an app.config file.

Application-level config files inherit settings from global configuration files like machine.config. Web also applications inherit settings from applicationHost.config.

Reading from the App.Config

Connection strings have a predefined schema that you can use. Note that this small snippet is actually a valid app.config (or web.config) file:

<?xml version="1.0"?>
<configuration>
    <connectionStrings>   
        <add name="MyKey" 
             connectionString="Data Source=localhost;Initial Catalog=ABC;"
             providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

Once you have defined your app.config, you can read it in code using the ConfigurationManager class. Don't be intimidated by the verbose MSDN examples; it's actually quite simple.

string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;

Writing to the App.Config

Frequently changing the *.config files is usually not a good idea, but it sounds like you only want to perform one-time setup.

See: Change connection string & reload app.config at run time which describes how to update the connectionStrings section of the *.config file at runtime.

Note that ideally you would perform such configuration changes from a simple installer.

Location of the App.Config at Runtime

Q: Suppose I manually change some <value> in app.config, save it and then close it. Now when I go to my bin folder and launch the .exe file from here, why doesn't it reflect the applied changes?

A: When you compile an application, its app.config is copied to the bin directory1 with a name that matches your exe. For example, if your exe was named "test.exe", there should be a ("text.exe.config" in .net framework) or ("text.dll.config" in .net core) in your bin directory. You can change the configuration without a recompile, but you will need to edit the config file that was created at compile time, not the original app.config.

1: Note that web.config files are not moved, but instead stay in the same location at compile and deployment time. One exception to this is when a web.config is transformed.

.NET Core

New configuration options were introduced with .NET Core and continue with the unified .NET (version 5+). The way that *.config files works hasn't fundamentally changed, but developers are free to choose new, more flexible configuration paradigms.

As with .NET Framework configuration .NET Core can get quite complex, but implementation can be as simple as a few lines of configuration with a few lines of c# to read it.

riQQ
  • 9,878
  • 7
  • 49
  • 66
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • 1
    See also: http://www.codeproject.com/Articles/16466/Unraveling-the-Mysteries-of-NET-Configuration for a great overview of how config files work – BenKoshy Nov 11 '15 at 02:58
68

Simply, App.config is an XML based file format that holds the Application Level Configurations.

Example:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="key" value="test" />
  </appSettings>
</configuration>

You can access the configurations by using ConfigurationManager as shown in the piece of code snippet below:

var value = System.Configuration.ConfigurationManager.AppSettings["key"];
// value is now "test"

Note: ConfigurationSettings is obsolete method to retrieve configuration information.

var value = System.Configuration.ConfigurationSettings.AppSettings["key"];
riQQ
  • 9,878
  • 7
  • 49
  • 66
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
  • 24
    You need to reference `System.Configuration.dll` in order to use the above mentioned APIs. – KFL Sep 09 '14 at 05:25
  • 2
    For one, you should not be storing connection strings in the AppSettings section, they go in the ConnectionStrings section. For two, Visual Studio will generate strong typed properties for your settings if you create them properly through the designer, you should never need to type out a key name manually (like you are in the above code). – BrainSlugs83 Jul 18 '16 at 20:44
  • 2
    We are not talking about the best practices here but rather the solution to OP problem. – Furqan Safdar Jul 18 '16 at 22:04
  • 3
    `System.Configuration.ConfigurationManager.AppSettings["Key"]` is a string already, calling `ToString()` on it is redundant. – Bogdan Stăncescu Jul 04 '17 at 12:52
6

App.Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.

See this MSDN article on how to do that.

TylerH
  • 20,799
  • 66
  • 75
  • 101
System Down
  • 6,192
  • 1
  • 30
  • 34
5

Just to add something I was missing from all the answers - even if it seems to be silly and obvious as soon as you know:

The file has to be named "App.config" or "app.config" and can be located in your project at the same level as e.g. Program.cs.

I do not know if other locations are possible, other names (like application.conf, as suggested in the ODP.net documentation) did not work for me.

PS. I started with Visual Studio Code and created a new project with "dotnet new". No configuration file is created in this case, I am sure there are other cases. PPS. You may need to add a nuget package to be able to read the config file, in case of .NET CORE it would be "dotnet add package System.Configuration.ConfigurationManager --version 4.5.0"

Dantel35
  • 103
  • 1
  • 5
  • 1
    this is what I was looking for. Needed to know what level to include it at. Some programs/frameworks look in folders like `/resources` – alex Mar 31 '20 at 01:22
3

You can access keys in the App.Config using:

ConfigurationSettings.AppSettings["KeyName"]

Take alook at this Thread

Community
  • 1
  • 1
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

Just adding one more point

Using app.config some how you can control application access, you want apply particular change to entire application use app config file and you can access the settings like below ConfigurationSettings.AppSettings["Key"]

0

Application settings enable you to store application information dynamically. Settings allow you to store information on the client computer that shouldn't be included in the application code (for example a connection string), user preferences, and other information you need at runtime.

  • To add an application configuration file to a C# project:

In Solution Explorer, right-click the project node, and then select Add > New Item. The Add New Item dialog box appears. Expand Installed > Visual C# Items. In the middle pane, select the Application Configuration File template. Select the Add button. A file named App.config is added to your project.

take a look at this article