NET 2.0中WinForm自定义的程序配置存放到哪里去了

.NET 2.0中,WinForm应用程序的配置已经非常方便。默认情况下,我们可以利用Properties文件夹里默认的Settings.setting文件进行Application和User两个层级配置信息的设置。在Settings.setting里进行的修改保存后,均自动在后部cs文件里自动生成相关代码,同时在应用程序配置文件(app.config)里存储相关信息。比如,我们在TestWinForm项目里对Settings.setting进行如下修改:



保存后,Settings.Designer.cs文件自动增加如下代码:

ContractedBlock.gif ExpandedBlockStart.gif Settings.Designer.cs
 1ExpandedBlockStart.gifContractedBlock.gifnamespace TestWinForm.Properties dot.gif{
 2InBlock.gif    
 3InBlock.gif    
 4InBlock.gif    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
 5InBlock.gif    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator""8.0.0.0")]
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase dot.gif{
 7InBlock.gif        
 8InBlock.gif        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
 9InBlock.gif        
10ExpandedSubBlockStart.gifContractedSubBlock.gif        public static Settings Default dot.gif{
11ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif{
12InBlock.gif                return defaultInstance;
13ExpandedSubBlockEnd.gif            }

14ExpandedSubBlockEnd.gif        }

15InBlock.gif        
16InBlock.gif        [global::System.Configuration.ApplicationScopedSettingAttribute()]
17InBlock.gif        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
18InBlock.gif        [global::System.Configuration.DefaultSettingValueAttribute("localhost")]
19ExpandedSubBlockStart.gifContractedSubBlock.gif        public string Server dot.gif{
20ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif{
21InBlock.gif                return ((string)(this["Server"]));
22ExpandedSubBlockEnd.gif            }

23ExpandedSubBlockEnd.gif        }

24InBlock.gif        
25InBlock.gif        [global::System.Configuration.ApplicationScopedSettingAttribute()]
26InBlock.gif        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
27InBlock.gif        [global::System.Configuration.DefaultSettingValueAttribute("2121")]
28ExpandedSubBlockStart.gifContractedSubBlock.gif        public int Port dot.gif{
29ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif{
30InBlock.gif                return ((int)(this["Port"]));
31ExpandedSubBlockEnd.gif            }

32ExpandedSubBlockEnd.gif        }

33InBlock.gif        
34InBlock.gif        [global::System.Configuration.UserScopedSettingAttribute()]
35InBlock.gif        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
36InBlock.gif        [global::System.Configuration.DefaultSettingValueAttribute("brooks")]
37ExpandedSubBlockStart.gifContractedSubBlock.gif        public string UserName dot.gif{
38ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif{
39InBlock.gif                return ((string)(this["UserName"]));
40ExpandedSubBlockEnd.gif            }

41ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{
42InBlock.gif                this["UserName"= value;
43ExpandedSubBlockEnd.gif            }

44ExpandedSubBlockEnd.gif        }

45InBlock.gif        
46InBlock.gif        [global::System.Configuration.UserScopedSettingAttribute()]
47InBlock.gif        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
48InBlock.gif        [global::System.Configuration.DefaultSettingValueAttribute("2006-05-01")]
49ExpandedSubBlockStart.gifContractedSubBlock.gif        public global::System.DateTime CreateDate dot.gif{
50ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif{
51InBlock.gif                return ((global::System.DateTime)(this["CreateDate"]));
52ExpandedSubBlockEnd.gif            }

53ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{
54InBlock.gif                this["CreateDate"= value;
55ExpandedSubBlockEnd.gif            }

56ExpandedSubBlockEnd.gif        }

57ExpandedSubBlockEnd.gif    }

58ExpandedBlockEnd.gif}

同时,app.config也发生了变化:

 1 None.gif <? xml version="1.0" encoding="utf-8"  ?>
 2 None.gif < configuration >
 3 None.gif     < configSections >
 4 None.gif         < sectionGroup  name ="applicationSettings"  type ="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"   >
 5 None.gif             < section  name ="TestWinForm.Properties.Settings"  type ="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  requirePermission ="false"   />
 6 None.gif         </ sectionGroup >
 7 None.gif         < sectionGroup  name ="userSettings"  type ="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"   >
 8 None.gif             < section  name ="TestWinForm.Properties.Settings"  type ="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  allowExeDefinition ="MachineToLocalUser"  requirePermission ="false"   />
 9 None.gif         </ sectionGroup >
10 None.gif     </ configSections >
11 None.gif     < applicationSettings >
12 None.gif         < TestWinForm .Properties.Settings >
13 None.gif             < setting  name ="Server"  serializeAs ="String" >
14 None.gif                 < value > localhost </ value >
15 None.gif             </ setting >
16 None.gif             < setting  name ="Port"  serializeAs ="String" >
17 None.gif                 < value > 2121 </ value >
18 None.gif             </ setting >
19 None.gif         </ TestWinForm.Properties.Settings >
20 None.gif     </ applicationSettings >
21 None.gif     < userSettings >
22 None.gif         < TestWinForm .Properties.Settings >
23 None.gif             < setting  name ="UserName"  serializeAs ="String" >
24 None.gif                 < value > brooks </ value >
25 None.gif             </ setting >
26 None.gif             < setting  name ="CreateDate"  serializeAs ="String" >
27 None.gif                 < value > 2006-05-01 </ value >
28 None.gif             </ setting >
29 None.gif         </ TestWinForm.Properties.Settings >
30 None.gif     </ userSettings >
31 None.gif </ configuration >

要在具体代码中使用配置信息就非常非常的方便了。

1 None.gif          private   void  button1_Click( object  sender, EventArgs e)
2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
3InBlock.gif            string msg = TestWinForm.Properties.Settings.Default.Server + ":" + TestWinForm.Properties.Settings.Default.Port.ToString();
4InBlock.gif            MessageBox.Show(msg);
5ExpandedBlockEnd.gif        }

OK,鬼扯了这么多,用意在于让我们再熟悉下.NET2.0的配置。现在,我们不满足他所提供的默认配置,于是我们创建了自己的一个Demo用的配置类 FtpSetting。在WinForm应用程序里,一切配置类都得继承自 ApplicationSettingsBase 类。

 1 None.gif      sealed   class  FtpSettings : ApplicationSettingsBase
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        [UserScopedSetting]
 4InBlock.gif        [DefaultSettingValue("127.0.0.1")]
 5InBlock.gif        public string Server
 6ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 7ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn (string)this["Server"]; }
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis["Server"= value; }
 9ExpandedSubBlockEnd.gif        }

10InBlock.gif
11InBlock.gif        [UserScopedSetting]
12InBlock.gif        [DefaultSettingValue("21")]
13InBlock.gif        public int Port
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn (int)this["Port"]; }
16ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis["Port"= value; }
17ExpandedSubBlockEnd.gif        }

18ExpandedBlockEnd.gif    }

如果要使用上述配置类,可以用:

1 None.gif          private   void  button2_Click( object  sender, EventArgs e)
2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
3InBlock.gif            FtpSettings ftp = new FtpSettings();
4InBlock.gif
5InBlock.gif            string msg = ftp.Server + ":" + ftp.Port.ToString();
6InBlock.gif            MessageBox.Show(msg);
7ExpandedBlockEnd.gif        }

好,似乎还在鬼扯。这个Tip已经进入尾声了,主题正式登场。:) 我们在使用上述 FtpSetting 配置时,当然要先进行赋值保存,然后再使用,后面再修改,再保存,再使用。
 1 None.gif          private   void  button2_Click( object  sender, EventArgs e)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            FtpSettings ftp = new FtpSettings();
 4InBlock.gif            ftp.Server = "ftp.test.com";
 5InBlock.gif            ftp.Port = 8021;
 6InBlock.gif
 7InBlock.gif            ftp.Save();
 8InBlock.gif            ftp.Reload();
 9InBlock.gif
10InBlock.gif            string msg = ftp.Server + ":" + ftp.Port.ToString();
11InBlock.gif            MessageBox.Show(msg);
12ExpandedBlockEnd.gif        }

嗯。已经Save了,你可能会在应用程序文件夹里找不到它到底保存到哪里去了。由于我们是用UserScope的,所以其实该配置信息是保存到了你的Windows的个人文件夹里去了。比如我的就是 C:\Documents and Settings\brooks\Local Settings\Application Data\TestWinForm目录了。哈~当作Tip吧,免得大伙一时找不到Save后的配置文件。

转载于:https://www.cnblogs.com/xiongeee/archive/2007/02/28/660039.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值