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 }
出处:ting blog
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器