开发者

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 t开发者_StackOverflow中文版he 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).

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.


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜