English 中文(简体)
添加价值以图谱和检索
原标题:
  • 时间:2009-05-29 11:52:54
  •  标签:

我需要添加关键数值。 概述如下:

<configuration>
 <appSettings>
    <add key="Setting1" value="Value1" />
    <add key="Setting2" value="Value2" />
 </appSettings>
</configuration>

当我用眼光进行搜查时,我看着以下法典。

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);

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?

最佳回答

难道你没有提及系统。 <代码>结构 经理人级。

EDIT: The System.Configuration namespace has imprisonment in mscorlib.dll, system.dll and in system.configuration.dll. 你们的项目始终包括斜体字和系统,但系统必须增加大部分项目类型,因为项目没有出现。

问题回答

这项工作:

public static void AddValue(string key, string value)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    config.AppSettings.Settings.Add(key, value);
    config.Save(ConfigurationSaveMode.Minimal);
}

添加“<代码>System.Configuration的参考资料,请通过提及“系统名称空间”获取一些配置名称空间,并增加该系统的参考。 配置应当使您能够进入<代码>ConfigurationManager。

我希望这项工作:

System.Configuration.Configuration config= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["Yourkey"].Value = "YourValue";
config.Save(ConfigurationSaveMode.Modified);

To Get The Data From the App.config

牢记:

  1. Added to the References -> System.Configuration
  2. and also added this using statement -> using System.Configuration;

只是这样做

string value1 = ConfigurationManager.AppSettings["Value1"];

或者,如果你不想增加<条码>使用系统,那么你就可以在一个行中做到这一点。

string value1 = System.Configuration.ConfigurationManager.AppSettings["Value1"]

对迟答复表示担忧,但我的准则可能有助于u。

我将3个县置于双赢表面。 but1 & 2将确定不同的价值,纽特3将收回现值。 因此,我的准则首先增加了参考系统。

and click on first button and then click on 3rd button to see what value has been set. next time again click on second & 3rd button to see again what value has been set after change.

这里就是该法典。

using System.Configuration;

 private void button1_Click(object sender, EventArgs e)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    config.AppSettings.Settings.Remove("DBServerName");
    config.AppSettings.Settings.Add("DBServerName", "FirstAddedValue1");
    config.Save(ConfigurationSaveMode.Modified);
}

private void button2_Click(object sender, EventArgs e)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    config.AppSettings.Settings.Remove("DBServerName");
    config.AppSettings.Settings.Add("DBServerName", "SecondAddedValue1");
    config.Save(ConfigurationSaveMode.Modified);
}

private void button3_Click(object sender, EventArgs e)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
          MessageBox.Show(config.AppSettings.Settings["DBServerName"].Value);
}




相关问题
热门标签