views:

2504

answers:

7

Is it possible to use a ".net configuration" file for a .NET console application?

I'm looking for an equivalent to web.config, but specifically for console applications...

I can certainly roll my own, but If I can use .NET's built in configuration reader then I would like to do that...I really just need to store a connection string...

Thanks

A: 

Yes, it's possible. You just need to make an app.config file.

Darren Kopp
A: 

Yes. Look up "application configuration file" in the documentation.

Robert Rossney
+12  A: 

Yes - use app.config.

Exactly the same syntax, options, etc. as web.config, but for console and WinForms applications.

To add one to your project, right-click the project in Solution Explorer, Add..., New Item... and pick "Application Configuration File" from the Templates box.

Dylan Beattie
+5  A: 

app.config... If you have an App.config in your project, it will get copied as executableName.exe.config in the case of a console application.

Greg Ogle
+1. Don't know why somebody voted this down - it's absolutely right.
Joe
A: 

SInce I haven't fully made the leap to TDD yet (though I hope to on some upcoming project) I use a console app to test my library code that I produce for another web developer in our company to use.

I use app.config for all of those settings, and as @Dylan says above the syntax is exactly the same between that and web.config, which means I can also just hand the content of my app.config over to the other dev and he can put them directly in his web.config. Very handy.

cori
A: 

I asked almost the same question some days ago and got really good answers, take a look:

Simplest way to have a configuration file in a Windows Forms C# Application

Fabio Gomes
A: 

This might help to some people dealing with Settings.settings and App.config: Watch out for GenerateDefaultValueInCode attribute in the Properties pane while editing any of the value (rows) in the Settings.settings grid in Visual Studio (VS2008 in my case). If you set GenerateDefaultValueInCode to True (True is the default here!), the default value is compiled into the exe (or dll), you can find it embeded in the file when you open it in a plain text editor. I was working on a console application and if I had defaults in the exe, the application always ignored the config file placed in the same directory! Quite a nightmare and no information about this on the whole internet.

Roman