Xml| Program 1 public class ApplicationSettings
2 {
3
4 private bool appsettingschanged;
5//variable for storing application settings.
6
7 Private point formlocation;
8
9 Public Point Formlocation
10 {
One get {return formlocation;}
Set
13 {
if (value!= formlocation)
15 {
Formlocation = value;
appSettingsChanged = true;
18}
19}
20}
21st
22
23//Deserialize the class from the configuration file.
public bool Loadappsettings ()
25 {
XmlSerializer myserializer = null;
FileStream myFileStream = null;
BOOL FileExists = false;
29
Try
31 {
32//Create XmlSerializer for applicationsettings type.
Myserializer = new XmlSerializer (typeof (ApplicationSettings));
FileInfo fi = new FileInfo (application.localuserappdatapath
+ @ "\myapplication.config");
36//If the configuration file exists, open it.
Panax Notoginseng if (fi. Exists)
38 {
myFileStream = fi. OpenRead ();
40//Deserialize the configuration file to create a new
A//ApplicationSettings instance.
ApplicationSettings myappsettings =
(ApplicationSettings) Myserializer.deserialize (
myFileStream);
45//For this instance of the ApplicationSettings class
46//Assign attribute value.
This.formlocation = myappsettings.formlocation;
FileExists = true;
49}
50}
Wuyi catch (Exception ex)
52 {
MessageBox.Show (ex. message);
54}
Finally
56 {
57//If FileStream is turned on, turn it off.
(myFileStream!= null)
59 {
Myfilestream.close ();
61}
62}
63
64
FileExists return;
66}
67
68//If the settings are changed, the
The 69//class is serialized into the configuration file.
public bool Saveappsettings ()
71 {
if (this.appsettingschanged)
73 {
StreamWriter mywriter = null;
XmlSerializer myserializer = null;
A try
77 {
78//For ApplicationSettings type
79//Create XmlSerializer.
Myserializer = new XmlSerializer (
Bayi typeof (ApplicationSettings));
Mywriter =
New StreamWriter (Application.LocalUserAppDataPath
+ @ "\myapplication.config", false);
85//This instance of the ApplicationSettings class
86//Serialization to the configuration file.
Myserializer.serialize (Mywriter, this);
88}
(Exception ex)
90 {
MessageBox.Show (ex. message);
92}
The finally
94 {
95//If FileStream is turned on, turn it off.
(Mywriter!= null)
97 {
Mywriter.close ();
99}
100}
101}
102 return appsettingschanged;
103}
104}
Source: Ting BLOG