如何在运行时在应用程序设置中存储键值对?

时间:2023-01-20 07:38:23

How can dynamic Key-value pairs of objects be stored in app.config in using the application settings api, at runtime?

如何在运行时使用应用程序设置api将动态键值对对象存储在app.config中?

I've been trying to get my head around, and I can't find any meaningful example or documentation.

我一直试图让自己的头脑,我找不到任何有意义的例子或文档。

I seems that .Net dictionary classes can't be serialized in XML to store them in app.config

我似乎.Net字典类不能用XML序列化以将它们存储在app.config中

Is the only way to do it by using custom XML serialized classes, or are there any other ways?

是通过使用自定义XML序列化类来实现它的唯一方法,还是有其他方法吗?

5 个解决方案

#1


2  

You ought to be able to do something like this:

你应该能够做这样的事情:

// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Add an Application Setting with a name Key and a Value stored in variable called Value
config.AppSettings.Settings.Add("Key", Value );

// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);

#2


2  

After some more searching net, I've found a very good (albeit very long) article which describes in dept the .Net configuration model:

经过一些搜索网,我发现了一篇非常好的(虽然很长)文章,它描述了.Net配置模型:

Unraveling the Mysteries of .NET 2.0 Configuration

揭开.NET 2.0配置的神秘面纱

I also found something very usefull:

我还发现了一些非常有用的东西:

Configuration Section Designer

配置部分设计器

A visual studio adding for visually designing config sections and generating the coresponging XSD files. I hope someone finds this useful.

一个视觉工作室,用于可视化地设计配置部分并生成核心XSD文件。我希望有人觉得这很有用。

#3


1  

Have always thought it was considered bad practice to change app.config during runtime. The app.config should be used for program setup and configuration; not for things that are dynamic that the application is actually modifying.

一直认为在运行时更改app.config被认为是不好的做法。 app.config应该用于程序设置和配置;不适用于应用程序实际修改的动态内容。

Suggest you create seperate "KeyValue.xml" file for dynamic objects.

建议您为动态对象创建单独的“KeyValue.xml”文件。

#4


0  

Depending on what exactly you need, you may try System.Collections.ObjectModel.KeyedCollection<TKey, TValue>. This only works if you can derive the key for an item from the item itself, but in that case, it's fine for this purpose.

根据您的具体需要,您可以尝试System.Collections.ObjectModel.KeyedCollection 。这只适用于您可以从项目本身派生项目的密钥,但在这种情况下,它可以用于此目的。 ,tvalue>

#5


0  

Is it a generic dictionary? If so:

它是通用字典吗?如果是这样:

XML Serializable Generic Dictionary

XML Serializable Generic Dictionary

#1


2  

You ought to be able to do something like this:

你应该能够做这样的事情:

// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Add an Application Setting with a name Key and a Value stored in variable called Value
config.AppSettings.Settings.Add("Key", Value );

// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);

#2


2  

After some more searching net, I've found a very good (albeit very long) article which describes in dept the .Net configuration model:

经过一些搜索网,我发现了一篇非常好的(虽然很长)文章,它描述了.Net配置模型:

Unraveling the Mysteries of .NET 2.0 Configuration

揭开.NET 2.0配置的神秘面纱

I also found something very usefull:

我还发现了一些非常有用的东西:

Configuration Section Designer

配置部分设计器

A visual studio adding for visually designing config sections and generating the coresponging XSD files. I hope someone finds this useful.

一个视觉工作室,用于可视化地设计配置部分并生成核心XSD文件。我希望有人觉得这很有用。

#3


1  

Have always thought it was considered bad practice to change app.config during runtime. The app.config should be used for program setup and configuration; not for things that are dynamic that the application is actually modifying.

一直认为在运行时更改app.config被认为是不好的做法。 app.config应该用于程序设置和配置;不适用于应用程序实际修改的动态内容。

Suggest you create seperate "KeyValue.xml" file for dynamic objects.

建议您为动态对象创建单独的“KeyValue.xml”文件。

#4


0  

Depending on what exactly you need, you may try System.Collections.ObjectModel.KeyedCollection<TKey, TValue>. This only works if you can derive the key for an item from the item itself, but in that case, it's fine for this purpose.

根据您的具体需要,您可以尝试System.Collections.ObjectModel.KeyedCollection 。这只适用于您可以从项目本身派生项目的密钥,但在这种情况下,它可以用于此目的。 ,tvalue>

#5


0  

Is it a generic dictionary? If so:

它是通用字典吗?如果是这样:

XML Serializable Generic Dictionary

XML Serializable Generic Dictionary