50073

How to use the read/writeable local XML settings?

Question:

I found something similar to what I need here: <a href="http://www.codeproject.com/KB/cs/PropertiesSettings.aspx" rel="nofollow">http://www.codeproject.com/KB/cs/PropertiesSettings.aspx</a> But it does not quite do it for me. The user settings are stored in some far away location such as C:\documents and settings\[username]\local settings\application data\[your application], but I do not have access to these folders and I cannot copy the settings file from one computer to another, or to delete the file altogether. Also, it would be super-convenient to have the settings xml file right next to the app, and to copy/ship both. This is used for demo-ware (which is a legitimate type of coding task) and will be used by non-technical people in the field. I need to make this quickly, so I need to reuse some existing library and not write my own. I need to make it easy to use and be portable. The last thing I want is to get a call at midnight that says that settings do not persist when edited through the settings dialog that I will have built.

So, user settings are stored god knows where, and application settings are read-only (no go). Is there anything else that I can do? I think app.config file has multiple purposes and I think I once saw it being used the way I want, I just cannot find the link.

Let me know if something is not clear.

Answer1:

You could create a class that holds your settings and then XML-serialize it:

public class Settings { public string Setting1 { get; set; } public int Setting2 { get; set; } } static void SaveSettings(Settings settings) { var serializer = new XmlSerializer(typeof(Settings)); using (var stream = File.OpenWrite(SettingsFilePath)) { serializer.Serialize(stream, settings); } } static Settings LoadSettings() { if (!File.Exists(SettingsFilePath)) return new Settings(); var serializer = new XmlSerializer(typeof(Settings)); using (var stream = File.OpenRead(SettingsFilePath)) { return (Settings)serializer.Deserialize(stream); } }

Recommend

  • QFileDialog for directories that have certain content
  • What is an image uri? Image uri inside the listview
  • How to track .lnk files in git?
  • How to save data in C/C++? [closed]
  • Is Python set more space efficient than list?
  • How to declare a new type from unions?
  • Linq: Find Element in a Collection
  • How to make Ruby's Find.find follow symlinks?
  • On reboot command router resets Openwrt Caos Calmer 15.05
  • lazy loading return null value
  • C# WPF Child Windows inside Main Window
  • Cannot read property “length” from undefined. (line 39, file “Code”)
  • ZKemKeeper: Events not triggering
  • Flask-Admin batch action with form
  • How to use boost spirit list operator with mandatory minimum amount of elements?
  • overriding equals and hashcode methods in java?
  • How to add a progress ring to the splash screen in Windows 8?
  • Why processBuilder in java hangs after 5 mins?
  • List using with references, changes behavior when used as a member
  • When i select a Textfield the keyboard moves over it
  • Jekyll - How do I create pages in the root directory?
  • How do I change the names of buttons on a message box?
  • Unity Resources.load() won't work with external dll
  • Unsupported ciphersuite TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  • Authorize Attribute Authentication with Postman in Web Api
  • PHP: Need to close STDIN in order to read STDOUT?
  • Create an Office365 mailbox from within C# Web API method
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • How to check if a database and tables exist in sql server in a vb .net project?
  • Multiplying polynomials/simplifying like terms
  • java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-ser
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • How to use Streams api peek() function and make it work?
  • How to decleare char *const argv[] in swift [duplicate]
  • How to get rgb from transparent pixel in js
  • WPF custom control and direct content support
  • media foundation H264 decoder not working properly