12

In a .NET Framework WinForms project, there was an App.config file in the project, which was an XML file that contained a configSection that would reference a class in System.Configuration, and a section for the userSettings themselves, like so:

<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561944e089">
        <section name="MyAppName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561944e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<userSettings>
    <MyAppName.Properties.Settings>
        <setting name="Test" serializeAs="String">
            <value>Some Value</value>
        </setting>
    </MyAppName.Properties.Settings>
</userSettings>

And this created a file in the build folder with the app name plus .exe.config, as in MyAppName.exe.config.

But when I create a new WinForms project using .NET:

New .NET WinForms Project

There is no App.config in the solution. I can edit the settings using the project properties:

App Settings on Project Properties designer

And I can access these values, and update them using the same Properties object and methods:

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            textBox1.Text = Properties.Settings.Default.Test;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Test = textBox1.Text;

            Properties.Settings.Default.Save();
        }
    }
}

And everything seems to work, but when I examine the bin folder, there is no file that I can see for where the values are actually stored.

Windows Explorer screen grab showing built application files

Where is .NET 5 storing the saved application settings if not in a file in the same folder as the application's exe?

Dave Slinn
  • 435
  • 6
  • 13

2 Answers2

8

User settings are stored in user.config file in the following path:

%userprofile%\appdata\local\<Application name>\<Application uri hash>\<Application version>

Application settings file are not created by default (unexpectedly), however if you create them manually beside the dll/exe file of your application, the configuration system respect to it. The file name should be <Application name>.dll.config. Pay attention to the file extension which is .dll.config.

You may want to take a look at the source code of the following classes:

At the time of writing this answer Application Settings for Windows Forms still doesn't have any entry for .NET 5 and redirects to 4.x documentations.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Do you know if it's still possible to do app.config transformations as described in https://stackoverflow.com/questions/3004210/app-config-transformation-for-projects-which-are-not-web-projects-in-visual-stud? This does not seem to still be working for me. – Dave Slinn Jan 08 '21 at 23:35
  • I haven't tried, but I expect it to work. I *guess* lack of creating application config file by default is a VS/designer related issue not the config system issue. – Reza Aghaei Jan 09 '21 at 10:57
0

First of all, this is a known (to .NET team) issue: https://github.com/dotnet/project-system/issues/7772.

Secondly the issue and the solution are pretty much described in your question:

(before) ..there was an App.config file in the project,.. (now) There is no App.config in the solution...

Add the missing app.config and everything will work just like it did before.

RussKie
  • 1,630
  • 11
  • 20