C# ApplicationSettings 部分和 AppSettings 部分有什么区别?

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

What is the difference between the ApplicationSettings section and the AppSettings section?

c#settings

提问by Calanus

Can someone please explain to me the difference between the AppSettings and ApplicationSettings sections in the App.Config file. Why are there two different sections that apparently do the same thing??

有人可以向我解释 App.Config 文件中 AppSettings 和 ApplicationSettings 部分之间的区别。为什么有两个不同的部分显然做同样的事情?

采纳答案by Saajid Ismail

I believe that the <appsettings/>collection in your app.config/web.config allows you to store settings in key-value pairs, and is accessed through the System.Configuration API, as follows:

我相信<appsettings/>app.config/web.config中的集合允许您将设置存储在键值对中,并通过 System.Configuration API 访问,如下所示:

string setting = System.Configuration.ConfigurationManager.AppSettings["settingName"];

string setting = System.Configuration.ConfigurationManager.AppSettings["settingName"];

Settings can only be stored and retrieved as string values.
They can also be accessed through System.Configuration.ConfigurationSettings, but this way has been deprecated.

设置只能作为字符串值存储和检索。
它们也可以通过 System.Configuration.ConfigurationSettings 访问,但这种方式已被弃用。

The <ApplicationSettings/>collection in your config file stores your settings in a strongly typed manner, and also allows you to access these settings in a strongly typed way. VS automatically generates wrapper classes for you, in the settings.settingsfile in the Propertiesfolder of your project. To add a settings file to your project, right click on your project, and click Properties, then open the Settingstab. Then click the link to add a new settings file. VS will automatically generate one for you. It's that easy.

<ApplicationSettings/>配置文件中的集合以强类型方式存储您的设置,并且还允许您以强类型方式访问这些设置。VS 会在项目的Properties文件夹中的settings.settings文件中自动为您生成包装类。要将设置文件添加到您的项目,请右键单击您的项目,然后单击Properties,然后打开Settings选项卡。然后单击该链接以添加新的设置文件。VS 会自动为你生成一个。就这么简单。

You usually access your settings as follows:

您通常按如下方式访问您的设置:

MyProjectName.Properties.Settings.Default.SettingName

MyProjectName.Properties.Settings.Default.SettingName

Notice the difference in how the two collections are accessed.

注意这两个集合的访问方式的不同。

The second (non-deprecated) way of storing settings is the better way to do it, and provides lots of flexibility and power. It takes some learning though - but it is worth the effort.

存储设置的第二种(非弃用)方式是更好的方式,并且提供了很大的灵活性和功能。虽然这需要一些学习 - 但值得付出努力。

回答by Matthew Vines

http://kevinskorner.net/blog/post/2008/03/27/applicationSettings-vs-appSettings.aspx

http://kevinskorner.net/blog/post/2008/03/27/applicationSettings-vs-appSettings.aspx

Application settings give us more control and most important, intelliscence.

应用程序设置为我们提供了更多的控制权,最重要的是,智能。

回答by Robert

It's to do with backwards compatibility, which we all love. ApplicationSettings is the newer construct.

这与我们都喜欢的向后兼容性有关。ApplicationSettings 是较新的构造。

User the newer ConfigurationManager and WebConfigurationManager classes to get at your settings now and to do this you need a reference so System.configuration and not System.Configuration :).

现在使用较新的 ConfigurationManager 和 WebConfigurationManager 类来获取您的设置,为此您需要一个参考,因此 System.configuration 而不是 System.Configuration :)。

At one point it started to get a bit silly didn't it.

有一段时间它开始变得有点傻了,不是吗。