Domanda

I'm predominantly a C# .NET 4.0 developer, and right now I'm reconsidering past approaches to storing and retrieving application settings.

In the past I've used a variety of methods, from simple 'flat' text files with tab separated key/values, to layered XML files, and a few other things in between. I've used in the past ( and am considering using again ) the built in App.Config/AppSettings classes, but there isn't any built in way to save data that you've loaded in from this source ( I could go into the difficulties I'm having in doing this but that's not the point of the question ).

The point here, is that all of these methods have their shortcomings, and while some work better than others, some are simpler to employ than others, and I am trying to nail down something that will be consistent and reliable.

My question, which will likely have a few valid answers so I'll be forced to pick the best one, is this: What if any built-in framework would 'you' recommend for both saving and loading data in and out of a settings file that can accompany the executable, or if not a built in framework, what standard would you recommend? XML? 'Flat' files? Stand-Alone Database?

Assume that the configuration itself won't be that large, and any real quantity of data will be read from a database ( or using some other technology ).

È stato utile?

Soluzione

While Im a registry fan, and would rather have my settings there, with my c# apps I've used the settings that come built in - so under project properties you can define a load, and their default values, and then access them with

Properties.Settings.Default.<settingname>

and can set them too, so Properties.Settings.Default.HideDisabled = true;

and you can save them with

Properties.Settings.Default.Save();

Ive not had issues with this, but then, as you said, everything has a pro and a con.

Altri suggerimenti

Just yet another way (kernel32), it's not much about C# even C# 4.0 but as we have started list of variants it must be valid here

namespace Ini {
    public class IniFile {
        private string path;
        [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 IniFile(string INIPath) {
            path = INIPath;
            }
        public void IniWriteValue(string Section, string Key, string Value) {
            WritePrivateProfileString(Section, Key, Value, this.path);
            }
        public string IniReadValue(string Section, string Key) {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp,
                                            255, this.path);
            return temp.ToString();
            }
        }
    }

A few applications I've worked on involve local databases and we used that database file to hold our settings too. It works, but something it just feels kinda wrong and it can be a problem if you would like to be able to modify a setting when your application is not running. (To bypass a crash or something alike.)

Most often I will just use an XML file stored in the Environment.SpecialFolder.ApplicationData folder and use a DataContractSerializer. It allows you to do some versioning, but most importantly, it's able to load and save XML files when certain properties on your settings object are new or non-existing. It won't crash the application and when you save your settings again it'll just save the latest version. I think it's clean, mean and simple.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top