What is the best way to store user preferences for a .NET application?

I have a Windows Forms.NET 2.0 application. Where is the best place for custom store settings (based on Windows recommendations)?

Some people pointed to Application.LocalUserAppDataPath . However, a folder structure is created, for example:

C: \ Documents and Settings \ username \ Local Settings \ Application Data \ company_name \ product_name \ product_version \

If I release version 1 of my application and save the XML file there, release version 2, which will change to another folder, right? I would prefer to have one folder for each user to store settings regardless of the version of the application.

+51
c #
Aug 25 '08 at 16:43
source share
8 answers

I like to use the built-in Application Settings . Then you have support for using the parameter constructor if you want at design time or at run time:

 // read setting string setting1 = (string)Settings.Default["MySetting1"]; // save setting Settings.Default["MySetting2"] = "My Setting Value"; // you can force a save with Properties.Settings.Default.Save(); 

It saves the settings in a similar folder structure as you describe (with the version in the path). However, with a simple call:

 Properties.Settings.Default.Upgrade(); 

The application will save all previous versions to save.

+73
Aug 25 '08 at 16:59
source share

.NET applications have a built-in settings engine that is easy to use. The problem with this, in my opinion, is that it saves these settings in a rather obscure directory, and end users will not be able to find it. Moreover, simply switching from debugging to assembly release changes the location of this directory, which means that any settings saved in one configuration are lost in another.

For these and other reasons, I came up with my own customization code for Windows Forms . It is not as smooth as the one that comes with .NET, but it is more flexible and I use it all the time.

+7
Mar 21 '11 at 15:00
source share

Or write your settings in an XML file and save it using Isolated Storage . Depending on the storage used, it saves it in the Application Data folder. You can also select a storage with roaming turned on, which means that when a user logs in to another computer, the settings move with them.

+4
Aug 25 '08 at 17:11
source share

One approach that worked for me in the past was to create a settings class and use XML serialization to write to the file system. You can extend this concept by creating a collection of settings objects and serializing it. You will have all the settings for all users in one place, without worrying about managing the file system.

Before anyone gives me any click to partially re-create the wheel, let me say a few things. Firstly, these are just a few lines of code for serializing and writing a file. Secondly, if you have an object that contains your settings, you do not need to make several calls to the appSettings object when the application loads. And finally, it’s very easy to add elements that represent the state of your applications, thereby allowing you to resume a lengthy task when the application loads further.

+2
Aug 25 '08 at 17:06
source share

Settings are standard key-value pairs (string-string). I could wrap them in an XML file if that helps.

I would prefer to use the file system instead of the registry. It seems easier to maintain. In support scenarios, if the user needs to manually open / change the settings, it would be easier if it were in the file system.

+1
Aug 25 '08 at 16:48
source share

I'm trying to use some methods to store my settings in a plain text file, and I found a better way:

stored in the application folder, for use, settings.txt : (comments are approved inside the settings file, try // comment)

// to get the settings value

 Settings.Get("name", "Ivan"); 

// to set the value of the settings

 Settings.Set("name", "John"); 

via:

 using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; 

// you can also save the section name to simply add the name to the Set (sector_name, name, value) and Get (section_name, name, value) sections

 public static class Settings { private static string SECTION = typeof(Settings).Namespace;//"SETTINGS"; private static string settingsPath = Application.StartupPath.ToString() + "\\settings.txt"; [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); public static String GetString(String name) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(SECTION,name,"",temp,255,settingsPath); return temp.ToString(); } public static String Get(String name, String defVal) { return Get(SECTION,name,defVal); } public static String Get(string _SECTION, String name, String defVal) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(_SECTION, name, "", temp, 255, settingsPath); return temp.ToString(); } public static Boolean Get(String name, Boolean defVal) { return Get(SECTION, name, defVal); } public static Boolean Get(string _SECTION, String name, Boolean defVal) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(_SECTION,name,"",temp,255,settingsPath); bool retval=false; if (bool.TryParse(temp.ToString(),out retval)) { return retval; } else { return retval; } } public static int Get(String name, int defVal) { return Get(SECTION, name, defVal); } public static int Get(string _SECTION, String name, int defVal) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(SECTION,name,"",temp,255,settingsPath); int retval=0; if (int.TryParse(temp.ToString(),out retval)) { return retval; } else { return retval; } } public static void Set(String name, String val) { Set(SECTION, name,val); } public static void Set(string _SECTION, String name, String val) { WritePrivateProfileString(_SECTION, name, val, settingsPath); } public static void Set(String name, Boolean val) { Set(SECTION, name, val); } public static void Set(string _SECTION, String name, Boolean val) { WritePrivateProfileString(_SECTION, name, val.ToString(), settingsPath); } public static void Set(String name, int val) { Set(SECTION, name, val); } public static void Set(string _SECTION,String name, int val) { WritePrivateProfileString(SECTION, name, val.ToString(), settingsPath); } } 
+1
Jun 25 '15 at 8:02
source share

I would go down the list of folders that you posted, except for the product version. You do not need reset settings after the release of the update.

I am really being removed from the registry for user settings due to the debugging factor / footprint. Currently, I save only a few basic settings (window size, position, version of the data file) in the registry, and I had problems if the update got worse or the user lost the second monitor, and this is where the application opened. Some of them are smart enough to understand regedit, but for the rest they need to do a reinstall, which is quick, but I think they are grumbling. With the file version, all I have to do is open the XML file in Notepad and do a quick setup.

In addition, I am looking for the application to start from a USB drive, and the settings associated with the file look much more friendly to this process. I'm sure I can make code to check / clean the registry, but I think most of us are already tired of the mess in the registry that seems to be eating our machines today.

I know there are some trade-offs with security, but none of the data I sort is critical for this reason, and I don't experience any performance issues due to the size of the application.

0
Aug 25 '08 at 16:50
source share

Sandbox storage is primarily used for applications distributed through ClickOnce and runs in a secure sandbox. The base path is resolved for you, and you cannot make a conclusion in your code. The path will be something like "\ LocalSettings \ ApplicationData \ IsolatedStorage \ ejwnwe.302 \ kfiwemqi.owx \ url.asdaiojwejoieajae ....", not everyone is so friendly. Your storage is also limited.

Ryan Farley is entitled .

0
Aug 25 '08 at 21:45
source share



All Articles