Write appSettings in external file

后端 未结 3 1098
野性不改
野性不改 2020-12-06 02:34

I have a config file app.exe.config and appSettings section has something like this:


    

        
3条回答
  •  青春惊慌失措
    2020-12-06 02:48

    A more complete answer to prevent confusion:

    Setup:

    1. Commandline project called 'app'
    2. app.exe.config file, App.config:

      
      
    3. App.Settings.config file with 'Copy to Output Directory'= 'Copy Always'

      
      
        
      
      
    4. Program.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();
      
      }
      

    This will result in App.Settings.config file updated to be on the filesystem as:

    
    
      
      
    
    

提交回复
热议问题