App.Config file in console application C#

user1240679 picture user1240679 · Apr 9, 2012 · Viewed 193k times · Source


I have a console application in which I want to write the name of a file.

Process.Start("blah.bat");

Normally, I would have something like that in windows application by writing the name of the file 'blah.bat' to Settings file in Properties.
However, here I didn't find any Settings file and I added an app.config for the same purpose.

I am not sure what to write here in app.config, that would lead to me to achieve similar thing as in Windows Forms.

For eg: In windows forms. Process.Start(Properties.Settings.Default.BatchFile);
where BatchFile is a string in settings file in Properties.

Answer

xxbbcc picture xxbbcc · Apr 9, 2012

You can add a reference to System.Configuration in your project and then:

using System.Configuration;

then

string sValue = ConfigurationManager.AppSettings["BatchFile"];

with an app.config file like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings>
       <add key="BatchFile" value="blah.bat" />
   </appSettings>
</configuration>