Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you embed app.config in C# projects?

Tags:

c#

When you compile a C# project, the app settings from app.config are saved along with the exe file. For example, if the program name is "solve_np.exe", there will be a "solve_np.exe.config" next to it, making it look less neat than I want it to. How can you embed it into the .exe?

I tried to set Build Action to Embed Resource, but that did not do the trick.

like image 383
Wamiduku Avatar asked Dec 04 '09 13:12

Wamiduku


People also ask

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.

Can we add app config file in class library?

NET Framework application in Visual Studio, an app. config file is added to your project. When you create a class library or a . NET Core project, such a file is not included, although it can be done afterward.

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.


2 Answers

Aha. I guess you're falling foul of Visual Studio automatically placing stuff in configuration that you DONT want configured by the end user.

In this case use resources. Simply add a new file of type .resx. With this you'll be able to add things like strings and images to the resource file. After you build this it should provide static access to the resources (it typically generates a code file from the resources for you so you don't have to access ResourceManager yourself).

E.G. If I made resources.resx with a string called MyConfigValue after a build I should be able to access this like:

textBox.Text = Resources.MyConfigValue;

If you want values that are kinda variable but shouldn't be configurable then this is the right place to put them.

HTH.

like image 123
Quibblesome Avatar answered Nov 15 '22 19:11

Quibblesome


It isn't unprofessional to have an app.config file shipped alongside your exe. I think the word you may be looking for is untidy. I personally don't find this is the case myself however everyone is different! Perhaps you could simply make the app.config file hidden by default?

Or another alternative is to create your own config file which you could save to the App Data folder or even storing the settings in the registry instead.

like image 44
James Avatar answered Nov 15 '22 21:11

James