如何从app.config中读取Windows服务默认计时器值

时间:2011-08-06 08:58:35

标签: .net-4.0 windows-services timer

请告诉我如何阅读以下app.config

中的TimerFrequency
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
    <section name="Project1WindowsService.MyService" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
    <Project1WindowsService.MyService>
        <setting name="TimerFrequency" serializeAs="String">
            <value>300000</value>
        </setting>
    </Project1WindowsService.MyService>
</applicationSettings>

1 个答案:

答案 0 :(得分:1)

Properties.Settings.Default.TimerFrequency将返回300000

编辑:

好的,让它上班但你必须对你的app.config进行一些手术。它应该看起来像

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
<!--        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >-->
            <section name="Project1WindowsService.MyService" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!--        </sectionGroup>-->
    </configSections>

<!--    <applicationSettings>-->

      <Project1WindowsService.MyService>
            <setting name="TimeFrequency" serializeAs="String">
                <value>300000</value>
            </setting>
        </Project1WindowsService.MyService>

<!--    </applicationSettings>-->
</configuration>

您可以按如下方式检索值:

ClientSettingsSection sec  = (ClientSettingsSection)ConfigurationManager.GetSection("Project1WindowsService.MyService");

var element = sec.Settings.Get("TimeFrequency").Value.ValueXml.InnerText;

元素的值是30000

别忘了添加对System.Configuration的引用。

干杯