120


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.

Morse
  • 8,258
  • 7
  • 39
  • 64
user1240679
  • 6,829
  • 17
  • 60
  • 89

3 Answers3

270

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>
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • 14
    `ConfigurationSettings.AppSettings["blah.bat"]` is available but gives a warning that this is obsolete. Whenn I try to use `ConfigurationManager` as above, I get an error saying ConfigurationManager does not exist in the current context. :-/ – user1240679 Apr 09 '12 at 06:19
  • 73
    Make sure that you also add a reference to System.Configuration - the `using` durective only works when you have a reference to the assembly you're trying to use. – xxbbcc Apr 09 '12 at 13:12
  • 1
    late comment but for me my mistake was that I forgot upper-casing 'S' from appSettings –  Apr 01 '19 at 17:45
  • how do I get an int or bool, Ie not a string? – Zapnologica May 06 '20 at 10:54
  • 1
    @Zapnologica Everything is a string, as far as `AppSettings` is considered. If you have settings strings that contain numbers, you'll have to parse them by calling `int.TryParse()` or the like. The same goes for any other value that you want to treat something other than a string. – xxbbcc May 06 '20 at 15:06
50

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")
Zin Min
  • 3,898
  • 1
  • 20
  • 24
7

use this

System.Configuration.ConfigurationSettings.AppSettings.Get("Keyname")
Stefan Ferstl
  • 5,135
  • 3
  • 33
  • 41
user5705992
  • 91
  • 1
  • 1
  • 1
    you can save your configuration on appsettion and for Fetch the value use example . – user5705992 Dec 22 '15 at 06:05
  • 2
    Provides support for runtime version 1.x. This is [obsolete](https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationsettings.appsettings?redirectedfrom=MSDN&view=netframework-4.7.2#System_Configuration_ConfigurationSettings_AppSettings) now. Please use `System.Configuration.ConfigurationManager.AppSettings` – wp78de Aug 17 '18 at 21:11
  • 1
    System.Configuration assembly is needed in references. – Muflix Oct 25 '18 at 11:31