mmattax
mmattax

Reputation: 27670

How can I set up a configuration file for .NET console applications?

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

Upvotes: 18

Views: 29062

Answers (7)

Roman
Roman

Reputation: 581

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.

Upvotes: 4

Fabio Gomes
Fabio Gomes

Reputation: 6022

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

Upvotes: 0

cori
cori

Reputation: 8810

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.

Upvotes: 0

oglester
oglester

Reputation: 6655

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.

Upvotes: 14

Dylan Beattie
Dylan Beattie

Reputation: 54140

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.

Upvotes: 33

Robert Rossney
Robert Rossney

Reputation: 96722

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

Upvotes: 0

Darren Kopp
Darren Kopp

Reputation: 77627

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

Upvotes: 0

Related Questions