Common app.config for multiple applications

前端 未结 3 409
囚心锁ツ
囚心锁ツ 2020-12-18 05:24

I have several C# console applications, which need to have the same set of settings. I want to avoid duplicity and avoid separate app.config for each applicatio

相关标签:
3条回答
  • 2020-12-18 05:50

    @Ran's answer is an option, but each application will still have its own config file after you build. At compile time they will be the same, but at deploy time they are copies.

    You can also open one application's config file from another application using: ConfigurationManager.OpenExeConfiguration(string)

    You can have an external config file that all applications reference using: ConfigurationManager.OpenMappedExeConfiguration

    And there's the option to using the Machine config file using: ConfigurationManager.OpenMachineConfiguration()

    0 讨论(0)
  • 2020-12-18 05:52

    You can load an external app.config using the code below:

    config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
    string logpath = config.AppSettings.Settings["Log.Path"].Value;
    

    And save settings as so:

    config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
    config.AppSettings.Settings["Log.Path"].Value = "C:\newpath";
    config.Save();
    

    You might have to have a master config within one of the applications and point the rest to this. Typically this method is considered bad practice though. There might be issues with different applications locking the file.

    0 讨论(0)
  • 2020-12-18 05:55

    Create one file called app.config. Put it in some place outside of your projects' directories, like up in the solution directory. Add it to your projects as a linked item with a relative path to the file. Set the right build action for this item (application configuration) in each project.

    Now when each project builds, the file will be copied to the project's output dir with the right name.

    0 讨论(0)
提交回复
热议问题