假設你已經創建了一個類庫。在這裏面你定義它是這樣一個設置屬性:
Properties.Settings.Default.ProjectName
Visual Studio中可能會自動爲你生成一些配置如下:
(app.config)中
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MyDllProject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<MyDllProject.Properties.Settings>
<setting name="ProjectName" serializeAs="String">
<value>MyDllproject</value>
</setting>
</MyDllProject.Properties.Settings>
</applicationSettings>
</configuration>
現在說你將這個程序集添加到一個項目中。而你訪問它的設置,你很可能得到MyDllproject
,因爲它的價值。儘管增加了任何配置。爲什麼?因爲當組件生成時,它是寫入它的。編寫的代碼是這樣的,在沒有配置覆蓋的情況下,使用在生成時在app.config中定義的代碼。
現在在你的目標項目,只需添加必要的部分配置文件中按以下模式
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<!-- start: copied from app.config of class library -->
<section name="MyDllProject.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
<!-- end: copied from app.config of class library -->
<!-- other sections may follow -->
</sectionGroup>
</configSections>
<applicationSettings>
<!-- remember to maintain the same order as how it was defined in the sectionGroup -->
<!-- start: copied from app.config of class librarly -->
<MyDllProject.Properties.Settings>
<setting name="ProjectName" serializeAs="String">
<value>ConsoleProjectB</value>
</setting>
</MyDllProject.Properties.Settings>
<!-- end: copied from app.config of class library -->
<!-- other configurations settings may follow -->
</applicationSettings>
</configuration>
完蛋了。
這裏是一個小樣本項目,我掛了相同的:http://sdrv.ms/16ksPef
你能張貼的web.config的副本,以及。 – 2011-02-09 13:08:52
你可以在web.config中顯示'connectionStrings'部分嗎? – Egor4eg 2011-02-09 13:12:52