0

I have a console app project that write some info to a common.app.config file which other library project will read from in runtime. The writing operation is as follows:

var configMap = new ExeConfigurationFileMap
{
    ExeConfigFilename = @"C:\CONSOLE_APP_PROJECT_PATH\common.app.config"
};

var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

config.AppSettings.Settings.Add("FullLogPath",combinedPath);
config.Save(ConfigurationSaveMode.Minimal,true);
                        
ConfigurationManager.RefreshSection("appSettings");

Thw writing operation is done. To access this common.app.config file, I tried the procedures in this link :

Reuse config values across projects

I have added the common.app.config file from the other library project as link to avoid copying. So whatever that is written in runtime will be reflected in the linked common.app.config file .After linking from other project , the .cproj file has the following update:

<Content Include="..\..\DiagnosticsServiceHost\common.app.config">
  <Link>common.app.config</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

The project that refers the common config has its own configuration file (App.config) and inside it I have made the following changes:

<appSettings file=".\bin\x86\Debug\common.app.config">

</appSettings>

But I cannot access the value of the key - FullLogPath in runtime from common.app.config with following snippet: (the value of FullLogPath is null)

public class AppSettingsParent
{
    protected AppSettingsParent()
    {
    }

    // Only meant to be used in testing environments
    private static AppSettingsSection AppSettingsSection { get; } = null;

    protected static string Get([CallerMemberName] string propertyName = null)
    {
        if (AppSettingsSection != null)
        {
            return AppSettingsSection.Settings[propertyName].Value;
        }

        return ConfigurationManager.AppSettings[propertyName];
    }
}


public class CommonAppSettings : AppSettingsParent
{
    private CommonAppSettings()
    {
    }
    
    public static string FullLogPath => Get();

}

 private static string InitLogFile(string ecu, string filename)
    {
        var folder = Path.Combine(CommonAppSettings.FullLogPath + alu + "\\");

        var absoluteFilePath = Path.Combine(folder.Trim(), filename.Trim() + "-" + alu.Trim() + ".log");

        return absoluteFilePath;
    }
sajis997
  • 1,089
  • 1
  • 15
  • 29
  • The config file should be in same folder as the dll. – jdweng Feb 19 '21 at 12:23
  • https://stackoverflow.com/questions/15181969/common-app-config-for-multiple-applications – MD. RAKIB HASAN Feb 19 '21 at 12:31
  • Does it mean that even the linked config file can be opened in the same manner with OpenMappedExeConfiguration ? – sajis997 Feb 19 '21 at 12:37
  • Are you asking about multiple project in the same application sharing configuration, or different applications sharing a config file? – JonasH Feb 19 '21 at 12:38
  • Multiple projects under one solution sharing common.app.config file – sajis997 Feb 19 '21 at 12:46
  • Sharing a readonly file is easy with a link. But debugging in VS, each project will execute in their own build folder. Maybe you should ensure all your debug launch profiles have the same current directory. – Jeremy Lakeman Feb 25 '21 at 00:49

0 Answers0