How to read Windows Service Default Timer value from app.config
Please let me know how to read the TimerFrequency
from the following app.config
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089开发者_Go百科" >
<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>
Properties.Settings.Default.TimerFrequency will give you back 300000
Edit:
Ok, got it to work but you'll have to perform a bit of surgery on your app.config. it should look like
<?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>
and you can retrieve the value as follows:
ClientSettingsSection sec = (ClientSettingsSection)ConfigurationManager.GetSection("Project1WindowsService.MyService");
var element = sec.Settings.Get("TimeFrequency").Value.ValueXml.InnerText;
The value of element is 30000
Dont forget to add reference to System.Configuration.
Cheers
精彩评论