.net app.config 是什么时候创建的,什么时候是 app.exe.config 和有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2173283/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:51:21  来源:igfitidea点击:

When is an app.config created, when an app.exe.config and what is the difference

.netwinformsconfigurationapp-config

提问by Nikos Steiakakis

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

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

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

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

这里的问题是,在某些情况下,应用程序会创建“appname”.config 文件,而在其他情况下会创建“appname”.exe.config。

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.

第一次执行程序时保存配置。这给我们带来了一个问题,我无法指定创建哪一个的时机。

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

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

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

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

回答by keyboardP

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

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

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

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