0

I recently ran into an issue when I tried to use a console app I conceived. I have defined settings in the project's Properties panel of VS2019 and when I wanted to test the app on a different computer where I never ran it on, I copied over the exe and the exe.config file, I edited the settings in the .config file to reflect the new environment, but when I ran the app, I saw it still started off with the old settings.

So when I looked into it, tried with both Reload() and Reset(), but it just wouldn't load from the .config file. I tried to look in AppData\Local\ConsoleAppName but the folder was nowhere to be found.

I then tried the publishing wizzard and experimented with various publication options, but nothing would make a difference, the only way I could change these settings was through the properties page in VS2019.

I wrote the following to do some tests:

using ConsoleApp12.Properties;
using System;
using System.Configuration;

namespace ConsoleApp12
{
    class Program
    {

        public static void PrintParametres()
        {
            var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
            Console.WriteLine(path);
            foreach (SettingsProperty lSetting in Settings.Default.Properties)
            {
                Console.WriteLine("{0} = {1}", lSetting.Name, lSetting.DefaultValue);
            }
        }
        static void Main()
        {
            Settings.Default.Reload();
            ConsoleKeyInfo cki;
            PrintParametres();
            do
            {
                cki = Console.ReadKey(true);
                switch (cki.Key)
                {
                    case ConsoleKey.P:
                        PrintParametres();
                        break;
                    default:
                        Console.WriteLine("Print P for settings, Ctrl-C or Q to quit.");
                        break;
                }
            }
            while (cki.Key != ConsoleKey.Q);
            Settings.Default.Save();
        }
    }
}

The only files it produces are the .exe, the .exe.config and the .pdb. In the original .config file I have:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ConsoleApp12.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
    <userSettings>
        <ConsoleApp12.Properties.Settings>
            <setting name="TestSetting" serializeAs="String">
                <value>Some text in setting</value>
            </setting>
        </ConsoleApp12.Properties.Settings>
    </userSettings>
</configuration>

And if I change the value into <value>Some other text in setting</value> the output for pathis always %userprofile%\appdata\local\AppName\AppName.exe_Url_2543oxfebn2t3ud4uo1qvikliy41i53h\1.0.0.0\user.config but even with system files visibles, I can't find that folder and the properties match what is defined at design but they don't reflect changes in the .config file.

There's something I really don't get in this situation. Maybe I didn't understand correctly in the documentation, or it's specific to Forms applications maybe? Right now it's a console app, but the goal is to make a service so what am I missing here? or should I proceed differently?

EDIT: This question has already been explored, the answers have been tested and didn't provide any solution.

  • Does this answer your question? [Where are the Properties.Settings.Default stored?](https://stackoverflow.com/questions/982354/where-are-the-properties-settings-default-stored) – 41686d6564 stands w. Palestine May 07 '20 at 10:59
  • No, it doesn't. I saw that when I started to investigate the issue, and used the path lookup sample to further dig into it, but like I said, the settings seem to be cached from design time and it doesn't seem to read the config file at any time. –  May 07 '20 at 11:33

1 Answers1

0

The settings set during design time are embedded into the executable as defaults (at least that's what I seem to get from Reflector:

[UserScopedSetting, DebuggerNonUserCode, DefaultSettingValue("Some text in setting")]
public string TestSetting
{
    get => 
        ((string) this["TestSetting"]);
    set => 
        (this["TestSetting"] = value);
}

And remain the active setting unless it is altered.

To be saved and used upon the next execution, they have to be saved with Save(), at which point, the user.config file is actually generated under AppData\Local\Appname, but the file isn't otherwise created if the settings are never changed and saved during runtime.

Also, the sample code polls these default settings instead which would remain the same as what was set during design time (hence it called DefaultValue which I overlooked after having run in circles trying to understand how it all works out.).

The settings stored in the application.exe.config file however, don't seem to be used at any time.