35

I want to store the connection string and some parameters in app.config file which we generaly do for windows aplication but I can't find app.config file for console application. So how should I use this file, how to add this file or there is some other work arroud for the same functionality. I am working in console application

Nits
  • 883
  • 4
  • 15
  • 30

5 Answers5

60

Right click on application->Go to Add->you will see the exact picture What i have attached here->Pick the Application Config File.alt text

PrateekSaluja
  • 14,680
  • 16
  • 54
  • 74
8

Well, the other answers might have worked for others, but for me, adding a settings file to the project's properties solved the problem - it actually serialized the settings (which are editable via a visual designer) to the config file for me. So this way, the config file approach showed in the other answers here didn't work for me, but instead creating a settings file did work.

The steps are:

Project (not solution) > Add > New Item > Settings File

Adding Settings File

In addition, you might want to have your settings available in your code with strongly-typed values. I did the following:

  1. renamed the settings file to something useful - mine was "Settings.settings"
  2. moved this file to the "Project > Properties" section
  3. double-clicked the settings file icon
  4. In the designer, added settings keys and values
  5. Viola! You have the settings available in your app, with strongly-typed values!

So, now I could access my settings like this (from my console app):

bool boolSetting = Properties.Settings.Default.is_debug_mode;

After compilation, I found that these settings are stored automatically in a file named "AssemblyName.exe.config", alongside the console binary itself in the Debug directory.

So, I think this is a cleaner, and more flexible way of creating and managing the app's config file.

NOTE: Am running Visual Studio Ultimate 2012, and am building a .NET 3.5 console app.

JWL
  • 13,591
  • 7
  • 57
  • 63
6

alt text

source: http://blog.nickgravelyn.com/2010/02/visual-studio-2010-xna-and-you/

Adeel
  • 19,075
  • 4
  • 46
  • 60
  • I can't use Online Templates for adding this file, I don't have permissions as well as any option for this.Also I know how to use online templates but I don't have any option for adding this file – Nits Aug 30 '10 at 04:50
  • @Nits - This is not an add-in, it's been in visual studio since day 1. It has nothing to do with the fact you are using a console app. The answer is correct. +1 – RPM1984 Aug 30 '10 at 05:17
  • I don't have Online Template option in my VS, I am sure about this. – Nits Aug 30 '10 at 05:50
  • 5
    Online Templates isn't selected in that screenshot, it just looks a bit like it is. What's actually selected is "Visual C# Items", with the lighter gray background. – Rob Sep 02 '10 at 14:44
1

Anyone using VS2017 above and if you are not seeing the option highlighted in image Application Config File Then most probably you have not installed .net desktop development tools.

The application configuration file template comes from .net desktop development workload, so you need to make sure you've installed it in VS2019/VS2022.(Go tools=>get tools and features=>make sure you have .net desktop development workload checked. Or Use Visual Studio Installer to install the required package.

raghuj
  • 11
  • 1
0

Had the same problem, just open the Project tab and press Add Form (Windows Forms) then search for config and you will find app.config

hi hi
  • 1