Common app.config for multiple applications

前端 未结 3 410
囚心锁ツ
囚心锁ツ 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: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.

提交回复
热议问题