如何在运行时使用循环获取属性设置的当前值 [英] How to get the current value of a Property Setting at run-time using a loop
问题描述
我有一个名为 A
的属性,它通过 Visual Studio 中的属性设置窗口(项目 → 属性 → 设置)设置为值 AAA
.
I have a Property named A
and it was set to the value AAA
via the Properties Setting Window in Visual Studio (Project → Properties → Settings).
我可以使用此循环获取属性 A
的原始值.
如果我更改值,比如 NewValue
,我可以使用代码获取新值:
I can obtain the original value for the property A
using this loop.
If I change the value, to say NewValue
, I can get the new value using the code:
Properties.Settings.Default.A
但是,在循环中,我不知道如何在不使用语法的情况下获取当前属性值:
However, within the loop, I don't know how to get the current property value without using the syntax:
Properties.Settings.Default.VariableName
例如:
Properties.Settings.Default.A= "NewValue";
Properties.Settings.Default.Save();
foreach (SettingsProperty _currentProperty in Properties.Settings.Default.Properties)
{
Console.WriteLine(_currentProperty.Name + "," + _currentProperty.DefaultValue.ToString());
}
上面的循环显示了属性的原始值(旧的默认值是 AAA
).
The above loop shows the original value of the property (old default value which was AAA
).
我检查了 user.config
文件并确保它显示 NewValue
.
I have checked the user.config
file and made sure it is showing NewValue
.
我假设必须有某种方式使用我不知道的属性或方法来引用当前值(也许我应该迭代另一个集合?).
I assume that there must be some way to refer to the current value using a property or a method I don't know (maybe I should iterate another collection?).
问题是,如何在上面的 foreach
循环中显示这个新值?
The question is, how to display this new value inside the foreach
loop above?
推荐答案
当前 Properties.Settings
值在 SettingsPropertyValueCollection 查询 SettingsProvider(如果没有为设置定义提供程序,则为默认提供程序,LocalFileSettingsProvider,使用).
The current Properties.Settings
values are returned in a SettingsPropertyValueCollection Class when querying a SettingsProvider (if no Provider is defined for a setting, the default Provider, LocalFileSettingsProvider, is used).
当前代码正在修改设置的值,然后在设置保存到本地存储后检查这些值.
该代码迭代 Properties.Settings.Default.Properties
集合,对应于 ApplicationSettingsBase.Properties 属性,SettingsPropertyCollection 集合,其中包含 SettingsProperty 对象,内部用于表示配置属性的元数据.
The current code is modifying the value of a Setting and then inspect these values after the settings have been saved to the local storage.
The code iterates the Properties.Settings.Default.Properties
collection, corresponding to the ApplicationSettingsBase.Properties property, a SettingsPropertyCollection collection, which contains SettingsProperty objects, used internally to represent the metadata of configuration properties.
此类只能返回关联设置的默认值.
This class can only return the Default value of the associated Setting.
要检查与属性设置"关联的当前值,代码应改为迭代由 ApplicationSettingsBase.PropertyValues 属性,用于枚举 SettingsPropertyValue
对象.
这些对象的 PropertyValue 属性返回当前分配给设置的运行时值.
To inspect the current values associated to Properties Settings, the code should instead iterate the aforementioned SettingsPropertyValueCollection
, returned by ApplicationSettingsBase.PropertyValues property, which enumerates the SettingsPropertyValue
objects.
The PropertyValue property of these objects returns the run-time value currently assigned to the Setting.
要返回当前值,然后可以修改代码:
To return the current values, the code can then be modified in:
foreach (SettingsPropertyValue prop in Properties.Settings.Default.PropertyValues) {
Console.WriteLine($"{prop.Name} -> {prop.PropertyValue}");
}
关于应用程序设置的一些注意事项:
A couple of notes about the Application Settings:
应用程序设置分为 2 个主要类别:
Application Settings are organized in 2 main categories:
应用程序设置(
Application
范围内的设置)
只读,没有关联本地存储,因此不能在运行时更改.这些设置存储在app.config
文件中,该文件被复制到[Application].exe.config
构建项目时的文件.
Application Settings (Settings in the
Application
scope)
Read-only, no local storage is associated, so cannot be changed at run-time. These settings are stored int theapp.config
files, which is the copied to the[Application].exe.config
file when a Project is built.
用户设置(User
范围内的设置)(适用于 Visual Studio 2017+)
这些设置与应用程序设置一起存储,但具有关联的本地存储文件,用于存储用户(或应用程序)可以在运行时更改的值:
2.1 一个user.config
文件存储在当前用户配置文件中
User Settings (Settings in the User
scope) (applies to Visual Studio 2017+)
These settings are stored along with the Application settings, but have associated local storage files, used to store values that a User (or the Application) can change at run-time:
2.1 A user.config
file stored in the current User profile in the
[User]/AppData/Local/[CompanyName]/[ProductName].exe_<hash>/[Assembly Version]
部分,如果设置的 Roaming
属性设置为 false
.
2.2 一个user.config
文件保存在当前的User profile中
section, if the setting's Roaming
attribute is set to false
.
2.2 A user.config
file stored in the current User profile in the
[User]/AppData/Roaming/[CompanyName]/[ProductName]/[File Version]
部分,如果设置 Roaming
属性设置为 true
.
section, if the setting Roaming
attribute is set to true
instead.
User
范围内设置的原始值,即 Default
值,在为设置分配新值时不会被修改.
The original values of settings in the User
scope, the Default
values, are not modified when a new value is assigned to the Setting.
用户范围中的设置可以保存在运行时分配的新值,使用 ApplicationSettingsBase.Save() 方法:
Properties.Settings.Default.Save();
应用程序可以使用 ApplicationSettingsBase.Reload() 方法:
Properties.Settings.Default.Reload();
要放弃对用户设置所做的所有更改,可以使用 ApplicationSettingsBase.Reset() 方法:
Properties.Settings.Default.Reset();
这篇关于如何在运行时使用循环获取属性设置的当前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!