无法使用ConfigurationManager读取设置

时间:2015-08-14 09:00:43

标签: c# app-config configurationmanager

我想在运行时从/向C#项目app.config文件读取/写入设置信息。 以下尝试失败:

  1. var s = ConfigurationManager.AppSettings["mySet"]; s为空
  2. var sect = ConfigurationManager.GetSection("testing.Properties.Settings") as AppSettingsSection; sect为null
  3. 下面是App.config文件内部视图:

    configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="testing.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
       <userSettings>
        <testing.Properties.Settings>
            <setting name="mySet" serializeAs="String">
                <value>a</value>
            </setting>
            <setting name="hisSet" serializeAs="String">
                <value>b</value>
            </setting>
        </testing.Properties.Settings>
    </userSettings>
    

    但是,我可以通过以下电话阅读此信息:

    var prop = Properties.Settings.Default.mySet;
    

    为什么使用ConfigurationManager类的代码会失败?

    PS。我将testing.exe.config存储在与testing.exe相同的目录下

0 个答案:

没有答案