How to use local XML settings for reading / writing?

I found something similar to what I need here: http://www.codeproject.com/KB/cs/PropertiesSettings.aspx But this is not quite for me. User settings are stored in some remote place, for example C:\documents and settings\[username]\local settings\application data\[your application] , but I do not have access to these folders, and I can not copy the settings file from one computer to another or even delete the file. In addition, it would be super-convenient to have the xml file settings next to the application and copy / send both. This is used to demonstrate (which is a legitimate type of coding problem) and will be used by non-technical people in this area. I need to do this quickly, so I need to reuse the existing library, and not write it myself. I need to make it easy to use and portable. The last thing I want is to get a call at midnight, which says that the settings are not saved when editing in the settings dialog box that I will create.

Thus, user settings are stored, God knows where, and application settings are read-only (no access). Is there anything else I can do? I think the app.config file has several purposes, and I think I once saw that it is used the way I want, I just can’t find the link.

Let me know if something is unclear.

+3
source share
1 answer

You can create a class that contains your settings, and then XML serializes 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); } } 
+5
source

Source: https://habr.com/ru/post/909079/


All Articles