This article discusses how to store settings in Windows window applications, especially for reading information. Users can modify this path, and developers want to save them for future use. Three solutions are considered in the article: configuration files (.config), registry and custom XML files.
In view of the limitations of .NET configuration files and registry and personal preferences, this article focuses on using
custom XML fileto store the configuration settings. Custom XML file method
To use custom XML files to achieve persistence, please follow the steps below:
Create a new XML file in the application folder, such as MySettings.xml.
public class Settings { public string Path { get; set; } public Settings(string path) { Path = path; } public void Save(string filename) { XmlSerializer serializer = new XmlSerializer(typeof(Settings)); using (TextWriter writer = new StreamWriter(filename)) { serializer.Serialize(writer, this); } } public static Settings Load(string filename) { XmlSerializer serializer = new XmlSerializer(typeof(Settings)); using (TextReader reader = new StreamReader(filename)) { return (Settings)serializer.Deserialize(reader); } } }
Settings settings = new Settings("C:\my\path"); settings.Save("MySettings.xml");
The above is the detailed content of How Can I Persist Windows Forms Application Settings Using a Custom XML File?. For more information, please follow other related articles on the PHP Chinese website!