none
How to load configuration file from specific folder and save it to application path with different file name? RRS feed

  • Question

  • In most case, my configuration file(myprog.exe.config) is located in the application folder.

    However, in some case, the configuration file can not exist, and should load from the <application path>\Cfg\sample.cfg.

    In this case, I should load configuration file from <application path>\Cfg\sample.cfg.

    If user change the setting value and press [Save] button from GUI, current configuration data should be saved as <application path>\myprog.exe.config.

    To do this, how could I make scenario?

    In some article, they say I need to call AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", ConfigFile);

    Is that correct way?

    If so, should I call AppDomain.CurrentDomain.SetData() before load configuration file and call again before save configuration file?

    Otherwise, use just before save configuration file?

    FYI, I'm using ConfigurationManager.OpenMappedExeConfiguration() to load configuration file with specific configuration file path.

    Thursday, March 28, 2019 5:19 PM

Answers

  • Hi Jeff0803,

    Thank you for posting here.

    For your question, I create a new configuration file App1.config.

    Here is my code.

     static void Main(string[] args)
            {
                ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
                configMap.ExeConfigFilename = @"App1.config";
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    
                // Add an Application Setting.
                config.AppSettings.Settings.Add("ModificationDate", DateTime.Now.ToLongTimeString() + " ");
                // Save the changes in App.config file.
                config.Save(ConfigurationSaveMode.Modified);
                // Force a reload of a changed section.
                ConfigurationManager.RefreshSection("appSettings");
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 1, 2019 8:34 AM
    Moderator