App.Config更改值

a12*_*773 40 c# app-config

这是我的App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="lang" value="English"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

使用此代码,我进行了更改

lang = "Russian";
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
     System.Configuration.ConfigurationManager.AppSettings.Set("lang", lang);
}
Run Code Online (Sandbox Code Playgroud)

但它没有改变.我做错了什么?

Kev*_*mey 88

AppSettings.Set不会将更改保留到配置文件中.它只是在内存中改变它.如果您打开一个断点System.Configuration.ConfigurationManager.AppSettings.Set("lang", lang);,并为System.Configuration.ConfigurationManager.AppSettings[0]您添加一个监视,当该行代码运行时,它会看到它从"英语"变为"俄语".

以下代码(在控制台应用程序中使用)将保留更改.

class Program
{
    static void Main(string[] args)
    {
        UpdateSetting("lang", "Russian");
    }

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

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

从这篇文章: http ://vbcity.com/forums/t/152772.aspx

上面提到的一个要点是,如果从调试器(在Visual Studio中)运行它,那么每次构建时都会覆盖app.config文件.测试此方法的最佳方法是构建应用程序,然后导航到输出目录并从那里启动可执行文件.在输出目录中,您还可以找到名为YourApplicationName.exe.config的文件,它是您的配置文件.在记事本中打开它,看看实际上已保存更改.

  • 对于更健壮的代码,您可以添加一个测试键是否已经存在:`if(configuration.AppSettings.Settings [key] == null)configuration.AppSettings.Settings.Add(key,value); else configuration.AppSettings.Settings [key] .Value = value;` (2认同)

fen*_*222 46

您不能使用AppSettings静态对象.试试这个

string appPath = System.IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location);          
string configFile = System.IO.Path.Combine(appPath, "App.config");
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();         
configFileMap.ExeConfigFilename = configFile;          
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

config.AppSettings.Settings["YourThing"].Value = "New Value"; 
config.Save(); 
Run Code Online (Sandbox Code Playgroud)

  • 但它重新启动...我在Form关闭时进行更改...当我再次运行程序时,没有任何更改 (2认同)
  • 在使用Visual Studio 2013 Ultimate的.Net(4.5)上,我可以使用System.Configuration但没有System.Configuration.Configuration这样的对象. (2认同)
  • 设置值时抛出NullReferenceException.虽然我的appSettings中有一个键,但config.AppSettings.Settings计数为零.我在c#4.5 (2认同)

Ami*_*eri 10

当使用" ConfigurationUserLevel.None "时,当您单击调试文件夹中的nameyourapp.exe时,您的代码会正确运行..
但是当你在visual stdio上开发应用程序时没有正确运行!! 因为"vshost.exe"运行.

以下参数解决了这个问题:" Application.ExecutablePath "

试试这个:(在VS 2012 Express For Desktop中测试过)

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["PortName"].Value = "com3";
config.Save(ConfigurationSaveMode.Minimal);
Run Code Online (Sandbox Code Playgroud)

我的英语不好,对不起.


Ste*_*pUp 6

这在WPF应用程序中对我有用:

string configPath = Path.Combine(System.Environment.CurrentDirectory, "YourApplication.exe");
Configuration config = ConfigurationManager.OpenExeConfiguration(configPath);
config.AppSettings.Settings["currentLanguage"].Value = "En";
config.Save();
Run Code Online (Sandbox Code Playgroud)

  • 你的代码也适合我.谢谢! (2认同)
  • @jned29 如果对您有帮助,请随时投票支持我的答案:)。 (2认同)