你如何在 C# 项目中嵌入 app.config?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1846975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you embed app.config in C# projects?
提问by Wamiduku
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?
编译 C# 项目时,app.config 中的应用程序设置与 exe 文件一起保存。例如,如果程序名称是“solve_np.exe”,那么它旁边就会有一个“solve_np.exe.config”,使它看起来没有我想要的那么整洁。你怎么能把它嵌入到.exe中?
I tried to set Build Action to Embed Resource, but that did not do the trick.
我试图将 Build Action 设置为 Embed Resource,但这并没有奏效。
采纳答案by Quibblesome
Aha. I guess you're falling foul of Visual Studio automatically placing stuff in configuration that you DONT want configured by the end user.
啊哈。我猜你会因为 Visual Studio 自动将你不想由最终用户配置的东西放在配置中而犯规。
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).
在这种情况下,请使用资源。只需添加一个 .resx 类型的新文件。有了这个,您就可以将字符串和图像之类的内容添加到资源文件中。在您构建它之后,它应该提供对资源的静态访问(它通常为您从资源生成一个代码文件,因此您不必自己访问 ResourceManager)。
E.G. If I made resources.resx with a string called MyConfigValue after a build I should be able to access this like:
EG 如果我在构建后使用名为 MyConfigValue 的字符串创建了 resources.resx,我应该能够像这样访问:
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.
哈。
回答by Zote
I agree with serhio darasd and Quibblesome but you can just delete "solve_np.exe.config" and it'll just use default configs.
我同意 serhio darasd 和 Quibblesome,但您可以删除“solve_np.exe.config”,它只会使用默认配置。
回答by James
It isn't unprofessionalto have an app.config file shipped alongside your exe. I think the word you maybe 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?
将 app.config 文件与您的 exe 一起提供并不是不专业的。我认为您可能正在寻找的词是不整洁的。我个人不觉得这是我自己的情况,但每个人都不一样!也许您可以简单地默认隐藏 app.config 文件?
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.
或者另一种选择是创建您自己的配置文件,您可以将其保存到 App Data 文件夹,甚至将设置存储在注册表中。
回答by Timothy Carter
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.
通常,配置文件的理论是它存储您在部署应用程序后可能想要更改的设置(例如用户首选项)。为此,您需要存储在应用程序外部的某个地方。如果您使用默认设置,您将获得“[您的].exe.config”。您可以查看许多其他选项,但如果您提供一种保存某种设置的机制,那么几乎每个选项都以写在某处的文件告终。
回答by DOK
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.
这是另一个需要考虑的因素。经验丰富的 .Net 开发人员习惯于在 Web 应用程序和桌面应用程序中使用配置文件的标准应用程序设计。如果您背离了这种做法,那么跟随您的任何开发人员都将更难以理解您所做的工作。即使是 web 服务器上的 sa 也可能因缺少配置文件而感到困惑。
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.
由于几乎每个人都是这样做的,因此您的代码在其他人看来并不“不整洁”。相反,如果没有看到预期的配置文件,那将是令人困惑的。
回答by cjbarth
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:
在考虑了这些评论和其他搜索中写的内容后,我决定处理我希望将我的实体框架连接字符串嵌入到我的可执行文件中而不是需要 MyApplication.exe.config 文件与我部署的解决方案的问题的最佳方法是创建一个像这样的派生类:
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 "e;
's with actual quotes.
我刚刚创建了一个连接字符串类型的应用程序设置,其中包含与 App.Config 文件中的字符串相同的字符串。我只需"e;
要用实际引号替换' 。
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.
然后,每当我想使用 MyEFDataContainer 时,Dim db As New MyEFDataContainer
我都会使用它Dim db As New MyEFDataContainerLocal
。