NET 2.0中WinForm自定义的程序配置存放到哪里去了
.NET 2.0中,WinForm應(yīng)用程序的配置已經(jīng)非常方便。默認(rèn)情況下,我們可以利用Properties文件夾里默認(rèn)的Settings.setting文件進(jìn)行Application和User兩個(gè)層級(jí)配置信息的設(shè)置。在Settings.setting里進(jìn)行的修改保存后,均自動(dòng)在后部cs文件里自動(dòng)生成相關(guān)代碼,同時(shí)在應(yīng)用程序配置文件(app.config)里存儲(chǔ)相關(guān)信息。比如,我們?cè)赥estWinForm項(xiàng)目里對(duì)Settings.setting進(jìn)行如下修改:
保存后,Settings.Designer.cs文件自動(dòng)增加如下代碼:
?1namespace?TestWinForm.Properties?{
?2????
?3????
?4????[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
?5????[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator",?"8.0.0.0")]
?6????internal?sealed?partial?class?Settings?:?global::System.Configuration.ApplicationSettingsBase?{
?7????????
?8????????private?static?Settings?defaultInstance?=?((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new?Settings())));
?9????????
10????????public?static?Settings?Default?{
11????????????get?{
12????????????????return?defaultInstance;
13????????????}
14????????}
15????????
16????????[global::System.Configuration.ApplicationScopedSettingAttribute()]
17????????[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
18????????[global::System.Configuration.DefaultSettingValueAttribute("localhost")]
19????????public?string?Server?{
20????????????get?{
21????????????????return?((string)(this["Server"]));
22????????????}
23????????}
24????????
25????????[global::System.Configuration.ApplicationScopedSettingAttribute()]
26????????[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
27????????[global::System.Configuration.DefaultSettingValueAttribute("2121")]
28????????public?int?Port?{
29????????????get?{
30????????????????return?((int)(this["Port"]));
31????????????}
32????????}
33????????
34????????[global::System.Configuration.UserScopedSettingAttribute()]
35????????[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
36????????[global::System.Configuration.DefaultSettingValueAttribute("brooks")]
37????????public?string?UserName?{
38????????????get?{
39????????????????return?((string)(this["UserName"]));
40????????????}
41????????????set?{
42????????????????this["UserName"]?=?value;
43????????????}
44????????}
45????????
46????????[global::System.Configuration.UserScopedSettingAttribute()]
47????????[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
48????????[global::System.Configuration.DefaultSettingValueAttribute("2006-05-01")]
49????????public?global::System.DateTime?CreateDate?{
50????????????get?{
51????????????????return?((global::System.DateTime)(this["CreateDate"]));
52????????????}
53????????????set?{
54????????????????this["CreateDate"]?=?value;
55????????????}
56????????}
57????}
58}
同時(shí),app.config也發(fā)生了變化:
?1<?xml?version="1.0"?encoding="utf-8"??>?2<configuration>
?3????<configSections>
?4????????<sectionGroup?name="applicationSettings"?type="System.Configuration.ApplicationSettingsGroup,?System,?Version=2.0.0.0,?Culture=neutral,?PublicKeyToken=b77a5c561934e089"?>
?5????????????<section?name="TestWinForm.Properties.Settings"?type="System.Configuration.ClientSettingsSection,?System,?Version=2.0.0.0,?Culture=neutral,?PublicKeyToken=b77a5c561934e089"?requirePermission="false"?/>
?6????????</sectionGroup>
?7????????<sectionGroup?name="userSettings"?type="System.Configuration.UserSettingsGroup,?System,?Version=2.0.0.0,?Culture=neutral,?PublicKeyToken=b77a5c561934e089"?>
?8????????????<section?name="TestWinForm.Properties.Settings"?type="System.Configuration.ClientSettingsSection,?System,?Version=2.0.0.0,?Culture=neutral,?PublicKeyToken=b77a5c561934e089"?allowExeDefinition="MachineToLocalUser"?requirePermission="false"?/>
?9????????</sectionGroup>
10????</configSections>
11????<applicationSettings>
12????????<TestWinForm.Properties.Settings>
13????????????<setting?name="Server"?serializeAs="String">
14????????????????<value>localhost</value>
15????????????</setting>
16????????????<setting?name="Port"?serializeAs="String">
17????????????????<value>2121</value>
18????????????</setting>
19????????</TestWinForm.Properties.Settings>
20????</applicationSettings>
21????<userSettings>
22????????<TestWinForm.Properties.Settings>
23????????????<setting?name="UserName"?serializeAs="String">
24????????????????<value>brooks</value>
25????????????</setting>
26????????????<setting?name="CreateDate"?serializeAs="String">
27????????????????<value>2006-05-01</value>
28????????????</setting>
29????????</TestWinForm.Properties.Settings>
30????</userSettings>
31</configuration>
要在具體代碼中使用配置信息就非常非常的方便了。
1????????private?void?button1_Click(object?sender,?EventArgs?e)2????????{
3????????????string?msg?=?TestWinForm.Properties.Settings.Default.Server?+?":"?+?TestWinForm.Properties.Settings.Default.Port.ToString();
4????????????MessageBox.Show(msg);
5????????}
OK,鬼扯了這么多,用意在于讓我們?cè)偈煜は?NET2.0的配置。現(xiàn)在,我們不滿足他所提供的默認(rèn)配置,于是我們創(chuàng)建了自己的一個(gè)Demo用的配置類(lèi) FtpSetting。在WinForm應(yīng)用程序里,一切配置類(lèi)都得繼承自 ApplicationSettingsBase 類(lèi)。
?1????sealed?class?FtpSettings?:?ApplicationSettingsBase?2????{
?3????????[UserScopedSetting]
?4????????[DefaultSettingValue("127.0.0.1")]
?5????????public?string?Server
?6????????{
?7????????????get?{?return?(string)this["Server"];?}
?8????????????set?{?this["Server"]?=?value;?}
?9????????}
10
11????????[UserScopedSetting]
12????????[DefaultSettingValue("21")]
13????????public?int?Port
14????????{
15????????????get?{?return?(int)this["Port"];?}
16????????????set?{?this["Port"]?=?value;?}
17????????}
18????}
如果要使用上述配置類(lèi),可以用:
1????????private?void?button2_Click(object?sender,?EventArgs?e)2????????{
3????????????FtpSettings?ftp?=?new?FtpSettings();
4
5????????????string?msg?=?ftp.Server?+?":"?+?ftp.Port.ToString();
6????????????MessageBox.Show(msg);
7????????}
好,似乎還在鬼扯。這個(gè)Tip已經(jīng)進(jìn)入尾聲了,主題正式登場(chǎng)。:) 我們?cè)谑褂蒙鲜?FtpSetting 配置時(shí),當(dāng)然要先進(jìn)行賦值保存,然后再使用,后面再修改,再保存,再使用。
?1????????private?void?button2_Click(object?sender,?EventArgs?e)
?2????????{
?3????????????FtpSettings?ftp?=?new?FtpSettings();
?4????????????ftp.Server?=?"ftp.test.com";
?5????????????ftp.Port?=?8021;
?6
?7????????????ftp.Save();
?8????????????ftp.Reload();
?9
10????????????string?msg?=?ftp.Server?+?":"?+?ftp.Port.ToString();
11????????????MessageBox.Show(msg);
12????????}
嗯。已經(jīng)Save了,你可能會(huì)在應(yīng)用程序文件夾里找不到它到底保存到哪里去了。由于我們是用UserScope的,所以其實(shí)該配置信息是保存到了你的Windows的個(gè)人文件夾里去了。比如我的就是 C:\Documents and Settings\brooks\Local Settings\Application Data\TestWinForm目錄了。哈~當(dāng)作Tip吧,免得大伙一時(shí)找不到Save后的配置文件。
總結(jié)
以上是生活随笔為你收集整理的NET 2.0中WinForm自定义的程序配置存放到哪里去了的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SQL Server 2005数据库日志
- 下一篇: 如何编写企业解决方案书(转)