本文介绍了设置文件与app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是很了解设置和配置文件之间的交互/差异.如果我将一个条目添加到设置文件,它也会被添加到app.config中.这是否意味着更改app.config中的值将更新设置?如果没有,如何更新实时应用程序中的设置?使用设置文件而不是直接将内容直接放入app.config的一般目的是什么?

I'm not really understanding the interaction/differences between settings and config files. If I add an entry to the settings file, it gets added to the app.config as well. Does this mean that changing the value in the app.config will update the settings? If not, how do I update settings in a live application? What's the general purpose of using a settings file instead of putting things directly in app.config?

推荐答案

据我所知,看来设置文件和设计器为配置文件提供了一种包装.设置设计器控制着一些值,这些值会进入配置文件,但还会修改Properties目录中的.settings文件,并更改Settings.Designer.cs文件中生成的代码.

From what I can tell by looking around, it seems the settings file and designer provide a sort of wrapper for the config file. The settings designer controls some values that go into the config file, but also modifies the .settings file in your Properties directory, and changes the generated code in your Settings.Designer.cs file.

请注意,在配置文件中,值没有任何类型,但在.settings文件中,它们却有.现在打开Settings.Designer.cs.您将看到在那里生成的代码将值从配置文件中取出,并将其转换为在.settings文件或设置设计器中指定的正确类型.因此,它为配置文件中的值提供了类型安全的接口.如果您更改配置文件中的值,那么下次您进入设置设计器时,它将询问您是否要更新.settings以反映这些更改.

Notice that in the config file, values don't have any types, but in the .settings file, they do. Now open Settings.Designer.cs. You'll see that the code generated there takes values out of your config file, and casts them to the proper types, as specified in your .settings file, or the settings designer. So it's providing a type safe interface to values in your config file. If you change a value in your config file, then the next time you go to the settings designer, it will ask you if you would like to update .settings to reflect those changes.

因此,为回答您的问题,一旦应用程序部署完毕,您可以更改配置文件中的值,只要可以将其强制转换为正确的类型即可,否则我猜测它在Settings.Designer.cs中将失败.我能看到的唯一目的似乎是类型安全和设计人员在处理配置文件XML方面的便利.

So to answer your question, once the app is deployed, you can change the value in the config file as long as it can be cast to the proper type, otherwise I'm guessing it will fail in Settings.Designer.cs. The only purpose that I can see seems to be type safety and convenience of a designer over handling the XML of the config file.

此外,请记住,由于config不是版本文件,因此安装程序不会在客户端计算机上更新它们.它不会让它to足任何用户更改.因此,一旦部署完成,如果您将自己的更改放入用户所需的配置中,他们将不得不手动复制更改.

Also as an aside, remember that because config is not a versioned file, installers will not update them on a client's machine. It won't allow it to stomp on any user changes. So once it's deployed, if you put your own changes into the config that users will need, they will have to copy the changes over manually.

这篇关于设置文件与app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:00