Had a delegate of mine ask the same question recently - here's my reply. (key point is System.Windows.Forms.Application.*UserAppDataPath* property which is designed especially for user-specific application settings):
------ A couple of guidelines. If you are storing user preferences then the application config file is *not* the right place. You should store it in the user's profile which is somewhere like: C:\Documents and Settings\<username>\Application Data Here you can store any sort of "settings file" you like - it doesn't have to be in any particular format. What I have done in the past is have a couple of routines save and load a dictionary (StringCollection) to and from and XML file (note: you could use System.Xml.Serialization.XmlSerializer to do this for you). To help you with this you have the System.Windows.Forms.Application.UserAppDataPath property (it will create the path if it doesn't exist) and then in that folder you can stick what you like. This setting should roam with the user in a properly configured Windows network. Here's some routines from a project I've done recently: they load and save a StringCollection to/from a file called "ui.config" in the user's profile: ============================================== /*********************************************************************/ private void saveAppSettings( StringDictionary settings ) { string appPath = Application.UserAppDataPath; string configFileName = Path.Combine(appPath, "ui.config"); XmlDocument appSettingsDoc = new XmlDocument(); XmlNode root = appSettingsDoc.AppendChild(appSettingsDoc.CreateNode (XmlNodeType.Element, "Settings", null)); foreach(string settingName in settings.Keys) { XmlElement setting = appSettingsDoc.CreateElement ("Setting"); setting.SetAttribute("name", settingName); setting.SetAttribute("value", settings[settingName]); root.AppendChild(setting); } appSettingsDoc.Save(configFileName); } /*********************************************************************/ private StringDictionary loadAppSettings () { string appPath = Application.UserAppDataPath; string configFileName = Path.Combine(appPath, "ui.config"); XmlDocument appSettingsDoc = new XmlDocument(); appSettingsDoc.Load(configFileName); StringDictionary settings = new StringDictionary(); foreach(XmlNode setting in appSettingsDoc.SelectNodes ("Settings/Setting")) { string name = setting.Attributes.GetNamedItem ("name").Value; string value = setting.Attributes.GetNamedItem ("value").Value; settings[name] = value; } return settings; } ============================================== If you really want to write to the application's config file then do something like this: ============================================== void saveSetting(string key, string value) { //find config file string configFilePath = Application.ExecutablePath + ".config"; //load up into XML DOM XmlDocument configDoc = new XmlDocument(); configDoc.Load(configFilePath); //locate nodes of interest XmlNode appSettingsNode = configDoc.SelectSingleNode ("/configuration/appSettings"); //TODO: create 'appSettings' node if it doesn't exist //Find node they're after XmlNode settingNode = appSettingsNode.SelectSingleNode("add [@key='"+key+"']"); //If found: set the value; if not: create it if(settingNode!=null) { settingNode.Attributes["value"].Value = value; } else { XmlElement addElement = configDoc.CreateElement("add"); addElement.SetAttribute("key", key); addElement.SetAttribute("value", value); configDoc.SelectSingleNode ("/configuration/appSettings").AppendChild(addElement); } //Save and done configDoc.Save(configFilePath); } ===================================== You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.