什么时候创建一个app.config,当一个app.exe.config和什么区别 [英] When is an app.config created, when an app.exe.config and what is the difference

查看:433
本文介绍了什么时候创建一个app.config,当一个app.exe.config和什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们创建了一个WinForms应用程序,并通过ConfigurationManager类存储一些配置。为了存储信息我使用

We have created a WinForms application and store some configurations via the ConfigurationManager class. In order to store the information I use

Configuration pConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
pConfig.AppSettings.Settings.Add("updates", szUpdatesURL);
pConfig.Save(ConfigurationSaveMode.Modified);

这里的问题是,在某些情况下,应用程序创建一个appname.config文件,有时候会出现一个appname.exe.config。

The problem here is that in some occasions the application creates an "appname".config file and in other occasions an "appname".exe.config.

这里我必须注意的是,配置文件默认不发货, strong>

Here I must note that a configuration file is not shipped by default since it is not always required.

配置在第一次执行程序时保存。这导致了我们的问题,我不能指定创建一个或另一个的时间。

The configurations are saved the first time the program is executed. This has caused us a problem, and I cannot specify the occasions when which one or the other is created.

我在同一台电脑上执行了测试,完全相同的.exe和我得到两个结果。这里发生了什么?

I have performed the tests, on the same pc, with the exact same .exe and I get both results. What's going on here?

两者之间有什么区别,如何指定应该创建哪一个?
非常感谢

What is the difference between the two, and how can I specify which one should be created? Many thanks

推荐答案

当您编译应用程序时,会自动为您创建appname.exe.config。这是应该分发给最终用户的文件(当然还有exe文件)。您在appname.config中设置的设置将传输到appname.exe.config。
它们本质上是相同的文件。 appname.config存在的原因是因为当可执行文件运行时,它的配置文件是简单的可执行文件的名称与 .config 后缀。但是,如果可执行文件的名称已更改,则必须手动更改exe.config文件的名称。因此,通过在编译时自动重命名,app.config可以将其名称更改为newappname.exe.config文件,CLR仍会将其替换。您可能会发现appname.exe.config文件是在bin目录中创建的。
我希望很清楚:)下面的链接可能会更深入地解释它。

The "appname.exe.config" is automatically created for you when you compile your application. This is the file that should be distributed to your end users (along with the exe file, of course). The settings you set in appname.config is transferred over to appname.exe.config. They are essentially the same files. The reason appname.config exists is because when the executable is run, it's config file is simple the executable's name with a .config suffix. However, if the executable's name changed, you would have to change the name of the exe.config file manually. Therefore, by automatically renaming at compile time, the app.config can change it's name to newappname.exe.config file and the CLR will still pick it up. You'll probably find that the appname.exe.config file is created in the bin directory. I hope that's clear :) The links below may explain it in slightly more depth.

有一个很好的解释 here 。另一个良好的读物是 CodePlex

There's a good explanation here. Another good read is on CodePlex.

这篇关于什么时候创建一个app.config,当一个app.exe.config和什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆