178

I thought I knew this, but today I'm being proven wrong - again.

Running VS2008, .NET 3.5 and C#. I added the User settings to the Properties Settings tab with default values, then read them in using this code:

myTextBox.Text = Properties.Settings.Default.MyStringProperty;

Then, after the user edits the value in the options dialog I save it like this:

Properties.Settings.Default.MyStringProperty = myTextBox.Text;
Properties.Settings.Default.Save();

My question is, where is this new value saved? the MyApp.exe.config file in the executable directory is not updated, it still contains the default values. Plus, as far as I can tell, none of the other files in that directory are updated either! However, when the program reads the value back in, it gets the changed value, so I know it's saved somewhere...

This isn't just academic, I needed to be able to manually edit the value this morning and got myself stumped when I couldn't find anything that was changing.

NetMage
  • 26,163
  • 3
  • 34
  • 55
DaveN59
  • 3,638
  • 8
  • 39
  • 51
  • 2
    It's important to note that the storage location may change between different versions of the framework or the OS. Don't hardcode anything programmatic to the storage location. – Greg D Jun 11 '09 at 17:16
  • If you are in visual studio, developing a new app, then check at AppData\Local\Microsoft\YOUR APPLICATION NAME File name is user.config. I am in Windows 7 – RasikaSam Mar 18 '15 at 00:59
  • or under the AppData\Local one – KansaiRobot Dec 13 '18 at 04:33
  • Something I was struggling with that led me to this question and others: user.config is not created until applicationsSettings.Save(); is called. (where applicationSettings is defined ApplicationSettings applicationSettings = new ApplicationSettings( this );). – amalgamate Aug 29 '19 at 17:26

10 Answers10

166

In order to work with newer versions of Windows' policy of only allowing read access by default to the Program Files folder (unless you prompt for elevation with UAC, but that's another topic...), your application will have a settings folder under %userprofile%\appdata\local or %userprofile%\Local Settings\Application Data depending on which version of Windows you're running, for settings that are user specific. If you store settings for all users, then they'll be in the corresponding folder under C:\users or C:\Documents and Settings for all user profiles (ex: C:\users\public\appdata\local).

bluish
  • 26,356
  • 27
  • 122
  • 180
jasonh
  • 29,297
  • 11
  • 59
  • 61
  • 3
    Also, the .config generated in Visual Studio, which ends up in the executable's folder, I believe is only used for debugging. When packaging up the final application, you don't include this .config, as it's generated the first time the user runs the application. – Will Eddins Jun 11 '09 at 17:23
  • 12
    Newer versions? This has been the case since windows 2000. You just got away with it because you were running as administrator. – Joel Coehoorn Jun 11 '09 at 17:25
  • That does it. It makes perfect sense now that it's explained to me - not sure why I didn't "get it" earlier, but... Anyway, yours was the first, most complete explanation, so you get the points. – DaveN59 Jun 11 '09 at 17:27
  • 2
    @Joel Coehoorn: True, however now in the administrator account in Windows Vista and on, you cannot get write access to Program Files without a UAC elevation prompt by default. That's the policy that changed. :) – jasonh Jun 11 '09 at 18:05
  • 2
    It should also be noted that the folder changes with every version upgrade - so the user loses their preferences every time you update the software. – Ian Boyd Nov 08 '11 at 21:04
  • 21
    Actually no, they don't. You need create a setting, name it something like "UpgradeNeeded" and default it to true. Then when your app starts, check this. If it's true, call Properties.Settings.Default.Upgrade(), .Save() and .Reload(). Then reset UpgradeNeeded to false and save. – jasonh Nov 20 '11 at 22:11
  • jasonh, I wish I had learned it 3 years before. Got tired of making app settings screenshots before any version update :) – tequilacat May 01 '18 at 19:16
  • @WillEddins What? Could you elaborate on that? – KansaiRobot Dec 13 '18 at 04:37
  • How does one store settings for all users? – InteXX Jul 27 '19 at 19:52
  • why do i find it in **foobar.exe.config**? i'm using vs2019 and .net 4.5. – Lei Yang Apr 09 '20 at 01:37
113

You can get the path programmatically:

using System.Configuration;  // Add a reference to System.Configuration.dll
...
var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
Stephen Oberauer
  • 5,237
  • 6
  • 53
  • 75
Akbaritabar
  • 1,285
  • 1
  • 9
  • 8
  • 1
    Hmm, I'm seeing situations where the path returned by this is NOT the path used by Settings. Any insight? – George Mauer Jul 13 '16 at 04:13
  • 1
    @GeorgeMauer You mean `Roaming` or perhaps `LocalLow` instead of `Local`? `ConfigurationUserLevel` depends on the user's profile being roaming or local. In companies it is often roaming. – Kay Zed Jul 30 '16 at 17:32
  • Linking to [this answer](http://stackoverflow.com/a/1804324/344541) and my comments under it, showing what the path looks like. – Kay Zed Jul 30 '16 at 17:35
  • I am finding this same answer everywhere, use ConfigurationManager.OpenExeConfiguration() - but that gives three options depending on which value of ConfigurationUserLevel you pass, which give three different results - which is it?. How do you find out, in a program, in code, at run time, exactly which file the program is getting its settings from? – Dave May 09 '20 at 16:48
  • 2
    None: Gets the Configuration that applies to all users, PerUserRoaming: Gets the roaming Configuration that applies to the current user, PerUserRoamingAndLocal: Gets the local Configuration that applies to the current user. – Akbaritabar May 10 '20 at 02:38
13

thanks for pointing me in the right direction. I found user.config located at this monstrosity: c:\users\USER\AppData\Local\COMPANY\APPLICATION.exe_Url_LOOKSLIKESOMEKINDOFHASH\VERSION\user.config.

I had to uprev the version on my application and all the settings seemed to have vanished. application created a new folder with the new version and used the default settings. took forever to find where the file was stored, but then it was a simple copy and paste to get the settings to the new version.

Jeremy Ehret
  • 129
  • 1
  • 2
  • How know LOOKSLIKESOMEKINDOFHASH? – NeoSvet Feb 10 '19 at 09:14
  • 1
    Properties.Settings.Default.Upgrade() call would do the same automatically. It restores the config from its previous version, just like @jasonh said in other reply above. – Alex Mar 15 '19 at 14:49
  • @NeoSvet and others that run into this, see this [SO question](https://stackoverflow.com/q/24660121/182821) for how to generate the hash. – Chris Dec 28 '20 at 22:34
7

it is saved in your Documents and Settings\%user%\Local Settings\Application Data......etc search for a file called user.config there

the location may change however.

Stan R.
  • 15,757
  • 4
  • 50
  • 58
  • 5
    Actually, the correct environment variable for user-specific properties is %userprofile%. Specifying Documents and Settings under Vista or 7 will result in either a missing folder or missing permissions to the folder it does find. – jasonh Jun 11 '09 at 17:21
  • It helped me alot... Thanks Stan – ksrds Nov 09 '20 at 12:33
3

if you use Windows 10, this is the directory:

C:\Users<UserName>\AppData\Local\

+

<ProjectName.exe_Url_somedata>\1.0.0.0<filename.config>

2

One of my windows services is logged on as Local System in windows server 2016, and I can find the user.config under C:\Windows\SysWOW64\config\systemprofile\AppData\Local\{your application name}.

I think the easiest way is searching your application name on C drive and then check where is the user.config

EvilDuck
  • 101
  • 1
  • 4
1

They are saved in YOUR_APP.exe.config, the file is saved in the same folder with YOUR_APP.exe file, <userSettings> section:

   <userSettings>
      <ShowGitlabIssues.Properties.Settings>
         <setting name="SavedUserName" serializeAs="String">
            <value />
         </setting>
         <setting name="SavedPassword" serializeAs="String">
            <value />
         </setting>
         <setting name="CheckSave" serializeAs="String">
            <value>False</value>
         </setting>
      </ShowGitlabIssues.Properties.Settings>
   </userSettings>

here is cs code:

public void LoadInfoLogin()
{
    if (Properties.Settings.Default.CheckSave)// chkRemember.Checked)
    {
        txtUsername.Text = Properties.Settings.Default.SaveUserName;
        txtPassword.Text = Properties.Settings.Default.SavePassword;
        chkRemember.Checked = true;
    }
...
Luke
  • 1,623
  • 3
  • 24
  • 32
  • 1
    That's where the default values are stored. The OP is asking about where updated values are stored. FWIW, I also would NOT recommend storing a password in user settings, since it's stored in clear text. – Dov Jul 18 '23 at 16:47
0

User-specific settings are saved in the user's Application Data folder for that application. Look for a user.config file.

I don't know what you expected, since users often don't even have write access to the executable directory in the first place.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
-1

For anyone wondering where the settings for apps from the Microsoft Store are, they are either in WindowsApps, which is very locked down, but you can get there by opening your app and then opening the file path with Task-Manager.

But it's more likely that they are saved in C:\Users\[USERNAME]\AppData\Local\Packages\[NUMBERS][COMPANY].[APPLICATION]_[RANDOMDATA]\LocalCache\Local\[COMPANY]\[APPLICATION].exe_Url_[RANDOMDATA]\[VERSION]\user.config.

haldo
  • 14,512
  • 5
  • 46
  • 52
Lukas
  • 169
  • 1
  • 2
  • 8
-20

There is a folder called "Properties" under your project root folder, and there are *.settings file under that folder. That's where it gets stored.

J.W.
  • 17,991
  • 7
  • 43
  • 76
  • 1
    it's right _during developement_. After deployment they go elsewhere. – Joel Coehoorn Jun 11 '09 at 17:24
  • 2
    i had this running in a development environment and the file remained unchanged. it only stores the default value there, not the updated. – Stan R. Jun 11 '09 at 17:51
  • 4
    Stan R is correct, it only stores the default value. Not the values you may change to during debugging. – Anonymous Type Aug 26 '10 at 04:46
  • That's just the settings editor. It's a simple table that edits Settings.Designer.cs, which is part of your source code. When you run the application, they are stored under your user profile. When you run the application from inside VS, they are stored in the same location as the executable. – Phil Rogers May 10 '23 at 15:40