18

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.

Wamiduku
  • 203
  • 1
  • 2
  • 5

6 Answers6

11

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.

Quibblesome
  • 25,225
  • 10
  • 61
  • 100
  • I rephrased the question in order not to upset people. – Wamiduku Dec 04 '09 at 13:41
  • I'm not upset. I just want to know _why_ you think it is unprofessional. This is probably key to why people (me included) are misunderstanding the reasoning behind your question. What kind of data are you storing inside your "config" file? – Quibblesome Dec 04 '09 at 13:50
  • 1
    Something like "less neat when there is no need for the user to change the config" is what I should have written from the beginning, not "unproffessional". My apologies for being unclear. The data that ends up in the .config is VS generated. In general, aaronls describes just what I wanted to say. – Wamiduku Dec 04 '09 at 14:30
  • Thx, I think I'll do that. Whenever VS puts something in the config, I'll make my own version of it as a resource and redirect the references from the config to the resource. – Wamiduku Dec 04 '09 at 15:24
  • That don't work for configuring Libraries that are based around the config file. I guess it's a limitation of the library if they depend on it, but it would have been nice to have a transparent way of embedding the file. – Didier A. Aug 23 '13 at 21:00
  • 1
    The only kind of data I'm storing inside my "config" file is that it's not supposed to run on .NET 4.0 because it requires 4.5.2 at least. Microsoft intentionally made it rather difficult to tell which version you're _actually_ running on, making work-arounds rather difficult. I'm sure other people who want to embed their app.config are facing similar annoyances and have very good reasons for embedding it. – Roman Starkov Feb 07 '15 at 09:35
  • I'm having a similar issue in that I need to get the supportedRuntimes in the app.config, but we only want to distribute the exe. So its still desirable to have an embedded app.config. – Craig Brett Feb 21 '17 at 15:35
7

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.

James
  • 80,725
  • 18
  • 167
  • 237
  • 1
    +1 for giving registry as an option which really answer's OP's question (not that I'd like this option very much). – Sorin Comanescu Dec 04 '09 at 14:38
  • 4
    There's a few things in .Net that are configured in the app.config by default, but you don't need for the user to change it. Things like Enterprise Library Cryptography key and algorithm for example. It would be nice to be able to choose to embed it in your exe, since it's easy for the user to lose the app.config and break the exe. – Didier A. Aug 23 '13 at 20:47
4

Here's another factor to consider. Seasoned .Net developers are accustomed to the standard application design, which incorporates using config files in both web apps and desktop apps. If you depart from that practice, you will make it more difficult for any developers who follow you to understand what you have done. Even sa's on the web server may be confused by the absence of a config file.

Since pretty much everybody does it this way, your code does not look "untidy" to others. On the contrary, it would be befuddling not to see a config file where one is expected.

DOK
  • 32,337
  • 7
  • 60
  • 92
2

Typically the theory of a configuration file, is that it stores settings you may want to change once you've deployed the application (for something like user preferences). To do this, you need to be storing somewhere external to your application. If you use the default setup, you get "[Your].exe.config". There are many other options you could look at, but nearly every one of them ends up with a file written somewhere, if you providing a mechanism that saves settings of some kind.

Timothy Carter
  • 15,459
  • 7
  • 44
  • 62
0

After considering what was written in these comments and other searching, I decided that the best way to handle my issue of wanting my Entity Framework connection string to be embedded into my executable instead of needing the MyApplication.exe.config file with my deployed solution was to created a derived class like such:

Public Class MyEFDataContainerLocal
   Inherits MyEFDataContainer

    Public Sub New()
        MyBase.New(My.Settings.MyEFContainerConnectionString)
    End Sub
End Class

I just created an Application Setting of type Connection String that contained the same string as what is found in the App.Config file. I just had to replace the &quote;'s with actual quotes.

Then whenever I wanted to use the MyEFDataContainer as in Dim db As New MyEFDataContainer I would just use Dim db As New MyEFDataContainerLocal instead.

cjbarth
  • 4,189
  • 6
  • 43
  • 62
0

I agree with serhio darasd and Quibblesome but you can just delete "solve_np.exe.config" and it'll just use default configs.

Zote
  • 5,343
  • 5
  • 41
  • 43