简体   繁体   English

添加新的设置运行时

[英]Add new setting runtime

I'm trying save settings for my application with tabs. 我正在尝试使用标签保存应用程序的设置。 Each tab show some data from path. 每个选项卡显示一些来自路径的数据。 I store it like this: 我这样存储它:

 private Dictionary<int, string> _listTabs = new Dictionary<int, string>();

when I create new tab, I add new Item in dictionary 当我创建新标签时,我在字典中添加新项目

 _listTabs.Add(listTabs.Count++,CurrentPath);

before closing programm I want to save this dictionary in settings: 关闭程序之前,我想将此字典保存在设置中:

foreach(KeyValuePair kvp in _listTabs)
{
  var property = new SettingsProperty(kvp.Key);
  property.DefaultValue = kvp.Value;
  Settings.Default.Properties.Add(property);
}
Settings.Defaut.Save();

But, it doesnt work. 但是,它不起作用。 Where are the mistakes? 错误在哪里?

You'd need to tell us what the error is that you are seeing if the application is failing for some reason. 您需要告诉我们错误是什么,您正在查看应用程序是否由于某种原因而失败。

Also in your code above, I can't see any method to Save the settings 同样在上面的代码中,我看不到任何保存设置的方法

If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code: 如果要在应用程序会话之间保留对用户设置的更改,请调用Save方法,如以下代码所示:

Properties.Settings.Default.Save(); Properties.Settings.Default.Save();

For more info on user settings take a look at Using Settings in C# with specific focus on the area of Using Settings at Run Time . 有关用户设置的更多信息,请查看C#中的“ 使用设置” ,特别关注“在运行时使用设置 ”区域。

Once you've established that this works, check the user settings file. 一旦确定可以使用,请检查用户设置文件。 This Answer shows you where the settings are saved to. 此答案显示设置保存到的位置。

When we create new settings at runtime, add to Properties collection and try to save it, the new settings do not actually get saved in the user.config file. 当我们在运行时创建新设置时,将其添加到Properties集合中并尝试保存它,新设置实际上并没有保存在user.config文件中。

I faced the same issue and fixed it as follow: 我遇到了同样的问题,并将其修复如下:

  1. Create a dummy property in settings at design time : This will ensure that the user.config file will be created and settings would be saved in it. 在设计时在设置中创建一个虚拟属性 :这将确保将创建user.config文件并将设置保存在其中。 Now when we call Properties.Settings.Default.Save(); 现在,当我们调用Properties.Settings.Default.Save();时, the default provider "LocalFileSettingsProvider" will save all the Properties in Settings.Default.Properties collections including the one added on runtime. 默认提供程序“ LocalFileSettingsProvider”将所有属性保存在Settings.Default.Properties集合中,包括在运行时添加的集合。

You will see that the new properties added to the collection at runtime are getting saved in the user.config file. 您将看到在运行时添加到集合的新属性将被保存在user.config文件中。 But there is a second issue here which is when try to load the values from the user.config. 但是这里还有第二个问题,那就是何时尝试从user.config加载值。

It will only load the value of the settings which are present at design time irrespective of other properties present in the user.config. 它将仅加载设计时存在的设置值,而不考虑user.config中存在的其他属性。

The fix I did for same is to add the property with some default value in the Properties collection again and then call following: 我所做的解决办法是再次在Properties集合中添加具有一些默认值的属性,然后调用以下命令:

2 Load the property values from user.config : 2 从user.config加载属性值

SettingsProvider provider = Properties.Settings.Default.Providers["LocalFileSettingsProvider"]; SettingsProvider提供程序= Properties.Settings.Default.Providers [“ LocalFileSettingsProvider”];

SettingsPropertyValueCollection propertyValueCollection = provider.GetPropertyValues( Properties.Settings.Default.Context, Properties.Settings.Default.Properties); SettingsPropertyValueCollection propertyValueCollection = provider.GetPropertyValues(Properties.Settings.Default.Context,Properties.Settings.Default.Properties);

This will load the value from the user.config into the PropertyValues collection and then those values can be used. 这会将user.config中的值加载到PropertyValues集合中,然后可以使用这些值。

Hope it helps... 希望能帮助到你...

I noticed that the last line of your code reads 我注意到您的代码的最后一行显示为

Settings.Defaut.Save();

I believe that there is a misspelling. 我相信拼写有误。 Defaut should be Default . Defaut应该是Default

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM