简体   繁体   中英

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

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.

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).

EG 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.

It isn't unprofessional to have an app.config file shipped alongside your exe. I think the word you may be looking for is . 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.

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.

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.

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.

我同意serhio darasd和Quibblesome的观点,但您可以删除“ solve_np.exe.config”,而仅使用默认配置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM