在app.config文件中写入值

Ran*_*ana 49 c#

任何人都可以请帮助我如何使用c#在app.config文件中设置/存储值,是否可以?

Amo*_*rni 42

请尝试以下代码:

    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    config.AppSettings.Settings.Add("YourKey", "YourValue");
    config.Save(ConfigurationSaveMode.Minimal);
Run Code Online (Sandbox Code Playgroud)

它对我有用:-)


D.G*_*.G. 23

在Framework 4.5上,ConfigurationManager的AppSettings.Settings ["key"]部分是只读的,因此我必须首先删除密钥,然后使用以下内容再次添加它:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

config.AppSettings.Settings.Remove("MySetting");
config.AppSettings.Settings.Add("MySetting", "some value");

config.Save(ConfigurationSaveMode.Modified);
Run Code Online (Sandbox Code Playgroud)

不用担心,如果您尝试删除不存在的密钥,则不会出现异常.

这篇文章给出了一些好的建议


小智 21

private static string GetSetting(string key)
{
    return ConfigurationManager.AppSettings[key];
}

private static void SetSetting(string key, string value)
{
    Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    configuration.AppSettings.Settings[key].Value = value;
    configuration.Save(ConfigurationSaveMode.Full, true);
    ConfigurationManager.RefreshSection("appSettings");
}
Run Code Online (Sandbox Code Playgroud)


Abr*_*mJP 10

如果您使用App.Config存储值<app Key="" Value="" /> 或CustomSections部分使用ConfigurationManager类,则使用XMLDocument类.

您可以使用CodeProject上发布的代码


Sta*_*ked 7

正如其他人提到的,你可以这样做ConfigurationManager.AppSettings.Settings.但是:Settings[key] = value如果密钥不存在,则使用将无效.
使用Settings.Add(key, value),如果键已经存在,它会加入新的价值,它的价值(S)由逗号分隔,像 <add key="myKey" value="value1, value2, value3" />

要避免这些意外结果,您必须处理两个方案

  • 如果存在具有给定密钥的条目?然后更新它的价值
  • 如果具有给定密钥的条目不存在?然后创建新条目(键,值)

public static void Set(string key, string value)
{
    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    var entry = config.AppSettings.Settings[key];
    if (entry == null)
        config.AppSettings.Settings.Add(key, value);
    else
        config.AppSettings.Settings[key].Value = value;

    config.Save(ConfigurationSaveMode.Modified);
}
Run Code Online (Sandbox Code Playgroud)

有关支票的更多信息entry == null,请查看此帖子.
希望这会对某人有所帮助.


Jah*_*mic 6

对于.NET 4.0控制台应用程序,这些都不适用于我.所以我使用下面的代码,它起作用了:

private static void UpdateSetting(string key, string value)
{
    Configuration configuration = ConfigurationManager.
        OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
    configuration.AppSettings.Settings[key].Value = value;
    configuration.Save();

    ConfigurationManager.RefreshSection("appSettings");
}
Run Code Online (Sandbox Code Playgroud)


小智 5

string filePath = System.IO.Path.GetFullPath("settings.app.config");

var map = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
try
{
    // Open App.Config of executable
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

    // Add an Application Setting if not exist

        config.AppSettings.Settings.Add("key1", "value1");
        config.AppSettings.Settings.Add("key2", "value2");

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

    // Force a reload of a changed section.
    ConfigurationManager.RefreshSection("appSettings");
}
catch (ConfigurationErrorsException ex)
{
    if (ex.BareMessage == "Root element is missing.")
    {
        File.Delete(filePath);
        return;
    }
    MessageBox.Show(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)

  • 此响应非常有用,因为它展示了如何打开任意命名的配置文件,而不仅仅是 *.exe.config 文件;与其他回应不同。 (2认同)