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 path
is 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.