Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.Config file in console application C#

Tags:

c#

console


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.

like image 416
user1240679 Avatar asked Apr 09 '12 05:04

user1240679


People also ask

Where is app config file located?

The application configuration file usually lives in the same directory as your application. For web applications, it is named Web. config. For non-web applications, it starts life with the name of App.

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.


2 Answers

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> 
like image 129
xxbbcc Avatar answered Oct 03 '22 19:10

xxbbcc


For .NET Core, add System.Configuration.ConfigurationManager from NuGet manager.
And read appSetting from App.config

<appSettings>   <add key="appSetting1" value="1000" /> </appSettings> 

Add System.Configuration.ConfigurationManager from NuGet Manager

enter image description here

ConfigurationManager.AppSettings.Get("appSetting1") 
like image 32
Zin Min Avatar answered Oct 03 '22 21:10

Zin Min