0

I am really having propblem with understanding why my app.exe is not picking up the updated value in app.exe.config

app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<applicationSettings>
        <appName.AppProperties>
            <setting name="file_location" serializeAs="String">
                <value>C:\users\test\desktop\file_location.csv</value>
            </setting>
        </appName.AppProperties>
    </applicationSettings>
</configuration>

I thought my application will read the new location once I restart the app in case of (UAT,Prod). Any idea why the file_location is still the same aka cached?

LionKing
  • 516
  • 5
  • 9
  • Does this answer your question? [Who copies app.config to app.exe.config?](https://stackoverflow.com/questions/697529/who-copies-app-config-to-app-exe-config) – Jiale Xue - MSFT Mar 07 '22 at 02:28
  • unfortunately not. My problem is my app.exe only getting the default value not reading from app.exe.config – LionKing Mar 07 '22 at 13:13
  • Have you tried the solutions below? – Jiale Xue - MSFT Mar 17 '22 at 08:03
  • Just in the past few weeks (today is March 17 2022), me and my team have noticed that the app.config values are not being reliably udpated when we run. We have to rebuild the solution. This is new behaviour and really causing headaches, especially since the most common change is to swap different environments. – Daniel Williams Mar 17 '22 at 14:46
  • @DanielWilliams and did you find a solution for this behavior? because it is exactly the same. I have to go back to the code and update the app.config and then rebuild again – LionKing Mar 21 '22 at 13:15
  • Didn't Karen Payne's method solve your problem? – Jiale Xue - MSFT Mar 30 '22 at 09:14

1 Answers1

0

Perhaps an alternate approach, create a key under appSettings.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
    <appSettings>
        <add key="file_location" value="C:\users\test\desktop\file_location.csv"/>
    </appSettings>
</configuration>

Add the following class to your project which has a get and set for file_location.

using System;
using System.Configuration;
using System.IO;
using static System.Configuration.ConfigurationManager;

namespace YourNamespace
{
    public class ApplicationSettings
    {
        public static void SetFileLocation(string fileName) => SetValue("file_location", fileName);
        public static string GetFileLocation => AppSettings["file_location"];
        public static void SetValue(string key, string value)
        {

            var appName = Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().Location);

            var configFile = Path.Combine(appName, 
                $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.exe.config");

            var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
            var config = OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] == null)
            {
                throw new Exception($"Key {key} does not exist");
            }

            config.AppSettings.Settings[key].Value = value;

            config.Save();

            RefreshSection("appSettings");

        }
    }
}

Console app test

namespace YourNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            var current = ApplicationSettings.GetFileLocation;
            Console.WriteLine($"Current path: {current}");
            ApplicationSettings.SetFileLocation(current == "C:\\users\\test\\desktop\\file.csv" ?
                "C:\\users\\test\\desktop\\file_location.csv" :
                "C:\\users\\test\\desktop\\file.csv");

            Console.WriteLine($"Current path after set: {ApplicationSettings.GetFileLocation}");
            Console.ReadLine();
        }
    }
}

In regards to updating, see the following

Karen Payne
  • 4,341
  • 2
  • 14
  • 31