1
public class ApplicationSettings
2
{
3
4
private bool appSettingsChanged;
5
// 用于存储应用程序设置的变量。
6
7
private Point formLocation;
8
9
public Point FormLocation
10
{
11
get { return formLocation; }
12
set
13
{
14
if (value != formLocation)
15
{
16
formLocation = value;
17
appSettingsChanged = true;
18
}
19
}
20
}
21
22
23
// 从配置文件中反序列化类。
24
public bool LoadAppSettings()
25
{
26
XmlSerializer mySerializer = null;
27
FileStream myFileStream = null;
28
bool fileExists = false;
29
30
try
31
{
32
// 为 ApplicationSettings 类型创建 XmlSerializer。
33
mySerializer = new XmlSerializer(typeof(ApplicationSettings));
34
FileInfo fi = new FileInfo(Application.LocalUserAppDataPath
35
+ @"\myApplication.config");
36
// 如果配置文件存在,将其打开。
37
if (fi.Exists)
38
{
39
myFileStream = fi.OpenRead();
40
// 反序列化配置文件以创建新的
41
// ApplicationSettings 实例。
42
ApplicationSettings myAppSettings =
43
(ApplicationSettings)mySerializer.Deserialize(
44
myFileStream);
45
// 为 ApplicationSettings 类的这一实例
46
// 分配属性值。
47
this.formLocation = myAppSettings.FormLocation;
48
fileExists = true;
49
}
50
}
51
catch (Exception ex)
52
{
53
MessageBox.Show(ex.Message);
54
}
55
finally
56
{
57
// 如果 FileStream 是打开的,将其关闭。
58
if (myFileStream != null)
59
{
60
myFileStream.Close();
61
}
62
}
63
64
65
return fileExists;
66
}
67
68
// 如果设置发生变化,则将
69
// 类序列化到配置文件中。
70
public bool SaveAppSettings()
71
{
72
if (this.appSettingsChanged)
73
{
74
StreamWriter myWriter = null;
75
XmlSerializer mySerializer = null;
76
try
77
{
78
// 为 ApplicationSettings 类型
79
// 创建 XmlSerializer。
80
mySerializer = new XmlSerializer(
81
typeof(ApplicationSettings));
82
myWriter =
83
new StreamWriter(Application.LocalUserAppDataPath
84
+ @"\myApplication.config", false);
85
// 将 ApplicationSettings 类的这一实例
86
// 序列化到配置文件中。
87
mySerializer.Serialize(myWriter, this);
88
}
89
catch (Exception ex)
90
{
91
MessageBox.Show(ex.Message);
92
}
93
finally
94
{
95
// 如果 FileStream 是打开的,将其关闭。
96
if (myWriter != null)
97
{
98
myWriter.Close();
99
}
100
}
101
}
102
return appSettingsChanged;
103
}
104
}
105
public class ApplicationSettings2
{3

4
private bool appSettingsChanged;5
// 用于存储应用程序设置的变量。6

7
private Point formLocation;8
9
public Point FormLocation10
{11
get { return formLocation; }12
set13
{14
if (value != formLocation)15
{16
formLocation = value;17
appSettingsChanged = true;18
}19
}20
}21

22

23
// 从配置文件中反序列化类。24
public bool LoadAppSettings()25
{26
XmlSerializer mySerializer = null;27
FileStream myFileStream = null;28
bool fileExists = false;29

30
try31
{32
// 为 ApplicationSettings 类型创建 XmlSerializer。33
mySerializer = new XmlSerializer(typeof(ApplicationSettings));34
FileInfo fi = new FileInfo(Application.LocalUserAppDataPath35
+ @"\myApplication.config");36
// 如果配置文件存在,将其打开。37
if (fi.Exists)38
{39
myFileStream = fi.OpenRead();40
// 反序列化配置文件以创建新的41
// ApplicationSettings 实例。42
ApplicationSettings myAppSettings =43
(ApplicationSettings)mySerializer.Deserialize(44
myFileStream);45
// 为 ApplicationSettings 类的这一实例46
// 分配属性值。47
this.formLocation = myAppSettings.FormLocation;48
fileExists = true;49
}50
}51
catch (Exception ex)52
{53
MessageBox.Show(ex.Message);54
}55
finally56
{57
// 如果 FileStream 是打开的,将其关闭。58
if (myFileStream != null)59
{60
myFileStream.Close();61
}62
}63

64
65
return fileExists;66
}67

68
// 如果设置发生变化,则将69
// 类序列化到配置文件中。70
public bool SaveAppSettings()71
{72
if (this.appSettingsChanged)73
{74
StreamWriter myWriter = null;75
XmlSerializer mySerializer = null;76
try77
{78
// 为 ApplicationSettings 类型79
// 创建 XmlSerializer。80
mySerializer = new XmlSerializer(81
typeof(ApplicationSettings));82
myWriter =83
new StreamWriter(Application.LocalUserAppDataPath84
+ @"\myApplication.config", false);85
// 将 ApplicationSettings 类的这一实例86
// 序列化到配置文件中。87
mySerializer.Serialize(myWriter, this);88
}89
catch (Exception ex)90
{91
MessageBox.Show(ex.Message);92
}93
finally94
{95
// 如果 FileStream 是打开的,将其关闭。96
if (myWriter != null)97
{98
myWriter.Close();99
}100
}101
}102
return appSettingsChanged;103
}104
}105



浙公网安备 33010602011771号