接前面命令行程序下的Settings,继续看看 Visual Studio 创建的 .NET应用程序 与 Settings 相关的东西
用VC#创建一个GUI程序后,就会有一个 Settings.settings 文件。
一旦我们通过VC#给它添加值后,在工程目录下会生成一个 app.config 文件。
那么,这两个东西究竟是什么关系?
添加两个字符串类型的配置值(一个应用程序级,一个是用户级)后:
<?xml version='1.0' encoding='utf-8'?> <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WpfApplication1.Properties" GeneratedClassName="Settings"> <Profiles /> <Settings> <Setting Name="thisIsAppSettings" Type="System.String" Scope="Application"> <Value Profile="(Default)">dbzhang800</Value> </Setting> <Setting Name="thisIsUserSettings" Type="System.String" Scope="User"> <Value Profile="(Default)">1+1=2</Value> </Setting> </Settings> </SettingsFile>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="WpfApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> </sectionGroup> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="WpfApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <userSettings> <WpfApplication1.Properties.Settings> <setting name="thisIsUserSettings" serializeAs="String"> <value>1+1=2</value> </setting> </WpfApplication1.Properties.Settings> </userSettings> <applicationSettings> <WpfApplication1.Properties.Settings> <setting name="thisIsAppSettings" serializeAs="String"> <value>dbzhang800</value> </setting> </WpfApplication1.Properties.Settings> </applicationSettings> </configuration>
尽管都是 xml 文件,两个文件的风格截然不同,后者app.config就是我们前一篇中介绍到了 Configuration 文件,从它的configSections中可以看出它有两个自定义段,分别叫applicationSettings 和 userSettings
操作这样的一个文件,我们可以创建ConfigurationManager命名空间中的ConfigurationSection的派生类,然后通过该类进行操作。
Settings.settings 看起来相当的简洁,而且和app.config相比,它包含有配置值的类型(比如:Type="System.String")。
而且,无论我们修改Settings.settings还是app.config,Visual studio都会提示我们,并自动同步到另一个文件中。
这样一来,在程序内就可以通过
Properties.Settings.Default.thisIsUserSettings = "999999999"; console.WriteLine(Properties.Settings.Default.thisIsAppSettings); Properties.Settings.Default.Save();
来操作配置文件了。
其中:应用程序级配置是只读的,而用户级是可读写的。
用户配置会被写入到
C:\Users\dbzhang\AppData\Local\WpfApplication1\WpfApplication1.vshost.ex_Url_nifqt3tilgkwjt1wb1fupctc0ddm3exq\1.0.0.0\user.config
这些文件中。
同一个工程中,可以添加多个 .settings 文件,每一个都会生成一个封装类,各对 app.config 中的一部分进行操作。
比如:在前面的基础上,添加新的
<?xml version='1.0' encoding='utf-8'?> <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WpfApplication2.Properties" GeneratedClassName="another"> <Profiles /> <Settings> <Setting Name="qter1" Type="System.String" Scope="Application"> <Value Profile="(Default)">abc</Value> </Setting> <Setting Name="qter2" Type="System.String" Scope="User"> <Value Profile="(Default)">efg</Value> </Setting> </Settings> </SettingsFile>
此时,app.config 文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="WpfApplication2.Properties.another" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> <section name="WpfApplication2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> </sectionGroup> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="WpfApplication2.Properties.another" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <userSettings> <WpfApplication2.Properties.another> <setting name="qter2" serializeAs="String"> <value>efg</value> </setting> </WpfApplication2.Properties.another> <WpfApplication2.Properties.Settings> <setting name="user1" serializeAs="String"> <value>dbzhang800</value> </setting> <setting name="user2" serializeAs="String"> <value>dbzhang801</value> </setting> </WpfApplication2.Properties.Settings> ...
每一个 settings 文件中,还可以有多个 Profile 存在,这个怎么玩呢?