问题描述
我需要按如下所示在app.Config中插入键值对:
I need to insert key value pairs in app.Config as follows:
<configuration>
<appSettings>
<add key="Setting1" value="Value1" />
<add key="Setting2" value="Value2" />
</appSettings>
</configuration>
在Google中搜索时,我得到了以下代码段
When I searched in google I got the following code snippet
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Add an Application Setting.
config.AppSettings.Settings.Add("ModificationDate",
DateTime.Now.ToLongTimeString() + " ");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
上面的代码不起作用,因为在我使用的System.Configuration命名空间中找不到ConfigurationManager。 NET 2.0。
如何以编程方式将键值对添加到app.Config并检索它们?
The above code is not working as ConfigurationManager is not found in System.Configuration namespace I'm using .NET 2.0.How to add key-value pairs to app.Config programatically and retrieve them?
推荐答案
您是否丢失了引用到System.Configuration.dll? ConfigurationManager
类位于此处。
Are you missing the reference to System.Configuration.dll? ConfigurationManager
class lies there.
编辑: System.Configuration
名称空间在mscorlib.dll,system.dll和system.configuration.dll中具有类。您的项目始终包含mscorlib.dll和system.dll引用,但是system.configuration.dll必须添加到大多数项目类型中,因为默认情况下它不存在...
The System.Configuration
namespace has classes in mscorlib.dll, system.dll and in system.configuration.dll. Your project always include the mscorlib.dll and system.dll references, but system.configuration.dll must be added to most project types, as it's not there by default...
这篇关于将值添加到app.config并检索它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!