Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use application config file in C#?

I am trying to use a config file in my C# console application. I created the file within the project by going New --> Application Configuration File, and naming it myProjectName.config. My config file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SSDirectory" value="D:\Documents and Settings\****\MyDocuments\****" />
</appSettings>
</configuration>

The code to access it looks like this:

private FileValidateUtil()
    {
        sSDirFilePath = ConfigurationSettings.AppSettings["SSDirectory"];
        if (sSDirFilePath == null)
            Console.WriteLine("config file not reading in.");
    }

Can anyone lend a hint as to why this is not working? (I am getting the error message.)

Thanks!!

badPanda

like image 851
badpanda Avatar asked Jun 08 '10 17:06

badpanda


People also ask

How do I use a config file?

A file with the . CFG or . CONFIG file extension is a configuration file used by various programs to store settings that are specific to their respective software. Some configuration files are plain text files but others might be stored in a format specific to the program.

What is app config in C?

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.

What is application config file?

An application configuration file is an XML file used to control assembly binding. It can redirect an application from using one version of a side-by-side assembly to another version of the same assembly. This is called per-application configuration.


2 Answers

You can't change the name from app.config and expect ConfigurationManager to find it without providing it more information. Change the name of myProjectName.config back to app.config, rebuild, and you will see a file in the bin folder called myProjectName.exe.config. Then your call to ConfigurationManager.AppSettings should work correctly.

like image 72
AJ. Avatar answered Sep 22 '22 15:09

AJ.


check the documentation

http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx

like image 40
Seattle Leonard Avatar answered Sep 23 '22 15:09

Seattle Leonard