Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the settings saved in a .NET 5 WinForms app?

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?

like image 406
Dave Slinn Avatar asked Dec 31 '20 07:12

Dave Slinn


People also ask

Where are .NET application settings stored?

settings is located in the My Project folder for Visual Basic projects and in the Properties folder for Visual C# projects.

Where are C# application settings saved?

User settings are saved in a file within a subfolder of the user's local hidden application data folder.

Where do you find application settings?

From the Home screen, tap the Apps icon (in the QuickTap Bar) > the Apps tab (if necessary) > Settings .

How do I access settings in C#?

This file is added to your project by default and can be found under the Properties folder within Visual Studio's Solution Explorer. The default name for this file is Settings. settings and it contains an easy to use interface for adding your application settings at design time.


1 Answers

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:

  • LocalFileSettingsProvider (The default setting provider)
  • ClientSettingsStore
  • ConfigurationManagerInternal
  • ClientConfigurationPaths

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.

like image 91
Reza Aghaei Avatar answered Oct 04 '22 08:10

Reza Aghaei