2015-05-11 42 views
3

我有這個配置文件如何檢索從userSettings部分在C#中的價值

<?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="DynamicFormWorker.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> 
    </sectionGroup> 
    </configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <userSettings> 
    <DynamicFormWorker.Properties.Settings> 
     <setting name="mandator" serializeAs="String"> 
     <value>$$mandator$$</value> 
     </setting> 
    </DynamicFormWorker.Properties.Settings> 
    </userSettings> 
    <appSettings> 
    <add key="log4net.Config" value="log4net.config" /> 
    <add key="ClientSettingsProvider.ServiceUri" value="" /> 
    </appSettings> 
    <system.web> 
    <membership defaultProvider="ClientAuthenticationMembershipProvider"> 
     <providers> 
     <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" /> 
     </providers> 
    </membership> 
    <roleManager defaultProvider="ClientRoleProvider" enabled="true"> 
     <providers> 
     <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" /> 
     </providers> 
    </roleManager> 
    </system.web> 
</configuration> 

現在我想通了如何在C#中加載一個具體的配置文件,但實際上我沒有得到我的價值強制要素。

我加載的exe配置這樣

 configLocation = new ExeConfigurationFileMap();    
     configLocation.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App.config"); 
     exeConfig = ConfigurationManager.OpenMappedExeConfiguration(configLocation, ConfigurationUserLevel.None); 

但我怎麼檢索userSettings裏面的委託人元素?由於

+0

雖然這個問題舉行會談寫入userSettings,它可能仍然是有趣的:http://stackoverflow.com/questions/629143/how-to-write-to-the-main-exes-config-usersettings-section – neontapir

回答

1

我並沒有斷言,這是實現這一價值(似乎太長了嗦!),但你可以做到這一點的「標準」或「接受」的方式:

ConfigurationSectionGroup userSettings = config.SectionGroups["userSettings"]; 
var settingsSection = userSettings.Sections["DynamicFormWorker.Properties.Settings"] as ClientSettingsSection; 
string mandator = settingsSection.Settings.Get("mandator").Value.ValueXml.InnerText; 
+0

我不幸得出了相同的結論,因爲我找不到任何替代方法。我認爲這是您需要通過'ConfigurationManager'訪問用戶設置的方式。 – Tarik

相關問題