在外部文件中编写应用设置

本文关键字:应用 设置 外部 文件 | 更新日期: 2023-09-27 18:35:31

我有一个配置文件app.exe.config和appSettings部分有这样的东西:

<configuration>
    <appSettings configSource="app.file.config" />
</configuration>

app.file.config 文件有这样的东西:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="var1" value="value 1" />
  <add key="var2" value="value 2" />
  <add key="var3" value="value 3" />
</appSettings>

我需要在运行时编辑 var1、var2 和 var3,我有这样的代码:

Configuration config = ConfigurationManager.OpenExeConfiguration("...path'app.exe);
config.AppSettings.SectionInformation.ConfigSource = "app.file.config";
config.AppSettings.Settings["var1"].Value = "value 11";
config.AppSettings.Settings["var2"].Value = "value 22";
config.AppSettings.Settings["var3"].Value = "value 33";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

当我运行配置时。救。。。。文件 app.file.config 有一个具有属性"file"的 appSettings 节点。此属性的值为 app.file.config

<appSettings file="app.file.config">
<add key="var1" value="value 1" />
  <add key="var2" value="value 2" />
  <add key="var3" value="value 3" />
</appSettings>

现在,如果我尝试加载配置文件,则会出现消息"无法识别的属性'文件'。请注意,属性名称区分大小写。

如果我手动删除文件属性,配置文件将正确加载。

有什么想法吗?

保存配置文件时如何避免写入文件属性。

谢谢

在外部文件中编写应用设置

使用外部配置文件对应用程序是透明的,

这部分还可以

</configuration>
    <appSettings configSource="app.file.config" />
</configuration>

还有这个:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="var1" value="value 1" />
  <add key="var2" value="value 2" />
  <add key="var3" value="value 3" />
</appSettings>

将代码更改为如下所示:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["var1"].Value = "value 11";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

引用外部配置文件对应用程序是透明的,所以你不必直接调用它。你可以使用配置管理器中的默认 AppSet 部分。

祝你好运

防止混淆的更完整答案:

设置:

  1. 名为"app"的命令行项目
  2. app.exe.config file, app.config:

    <appSettings file="App.Settings.config"></appSettings>
    
  3. 具有"复制到输出目录"="始终复制"的 App.Settings.config 文件

    <?xml version="1.0" encoding="utf-8"?>
    <appSettings>
      <add key="test" value="OVERRIDDEN"/>
    </appSettings>
    
  4. 节目.cs:

    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Local Config sections");
            var exepath = (new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).LocalPath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(exepath);
            config.AppSettings.SectionInformation.ConfigSource = "App.Settings.config";
            Console.WriteLine("BEFORE[test]=" + config.AppSettings.Settings["test"].Value);
            Console.WriteLine($"BEFORE[testExternalOnly]={config.AppSettings.Settings["testExternalOnly"]?.Value}");
            //to avoid: Error CS0266
            //Explicitly cast 'System.Configuration.AppSettingsSection'
            AppSettingsSection myAppSettings = (AppSettingsSection)config.GetSection("appSettings");
            myAppSettings.Settings["test"].Value = "NEW";
            if (!myAppSettings.Settings.AllKeys.Contains("testExternalOnly"))
                myAppSettings.Settings.Add("testExternalOnly", "NEWEXTERNAL");
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            //Read updated config
            Console.WriteLine("AFTER[test]=" + config.AppSettings.Settings["test"].Value);
            Console.WriteLine("AFTER[testExternalOnly]=" + config.AppSettings.Settings["testExternalOnly"].Value);
            Console.WriteLine("AFTER CONFIG EXTERNAL FILE: " + System.IO.File.ReadAllText("App.Settings.config"));
            Console.WriteLine("AFTER CONFIG FILE: " + System.IO.File.ReadAllText(System.AppDomain.CurrentDomain.FriendlyName + ".config"));
    
            //Shut current config
            config = null;
            //Open config
            config = ConfigurationManager.OpenExeConfiguration(exepath);
            config.AppSettings.SectionInformation.ConfigSource = "App.Settings.config";
            Console.WriteLine("AFTER[test]=" + config.AppSettings.Settings["test"].Value);
            Console.WriteLine("AFTER[testExternalOnly]=" + config.AppSettings.Settings["testExternalOnly"].Value);
            Console.WriteLine("AFTER CONFIG EXTERNAL FILE: " + System.IO.File.ReadAllText("App.Settings.config"));
            Console.WriteLine("AFTER CONFIG FILE: " + System.IO.File.ReadAllText(System.AppDomain.CurrentDomain.FriendlyName + ".config"));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.WriteLine("press the ENTER key to end");
        Console.ReadLine();
    }
    

这将导致 App.Settings.config 文件更新为在文件系统上,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<appSettings>
  <add key="test" value="NEW" />
  <add key="testExternalOnly" value="NEWEXTERNAL" />
</appSettings>

将配置文件声明如下:

<appSettings configSource="app.file.config">
<add key="var1" value="value 1" />
  <add key="var2" value="value 2" />
  <add key="var3" value="value 3" />
</appSettings>

从代码

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
AppSettingsSection myAppSettings = config.GetSection("appSettings")
myAppSettings.Settings["var1"].Value = "value 11";
config.Save(ConfigurationSaveMode.Modified);

请注意,我使用GetSection("appSettings")而不是配置。应用设置.设置

感谢所有帮助StackOverflow的人们。