I have one Windows Polling Service to send an automatic email each 10 minutes. I use Thread.Sleep(new TimeSpan(0, 10, 0));
to sleep the thread for 10 minutes, which is hard coded right now.
To avoid hard coding, I have tried App.config
which was not successful. I want to move the hard coding to some .ini
files. How can I read .ini
file from C# Windows Service.
EDIT: I try the below code to read from my windows service.
string pollingInterval = (string)new AppSettingsReader().GetValue("PollingInterval", typeof(string));
Gives the below error. Configuration system failed to initialize
app.config
is better solution than INI file.
Your app.config
file look something like below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
.......
<appSettings>
<add key="TimerInterval" value="10" />
</appSettings>
.......
</configuration>
And you read it like:
int timerInterval = Convert.ToInt32(ConfigurationManager.AppSettings["TimerInterval"]);
You need to import namespace using System.Configuration;
and add reference to System.Configuration dll.
Using App.config is as simple as
string interval = ConfigurationManager.AppSettings["interval"];
TimeSpan t;
TimeSpan.TryParseExact(interval, @"h\:m\:s", CultureInfo.InvariantCulture, out t);
(Don't forget to add the reference System.Configuration
assembly and using System.Configuration
+ System.Globalization
)
Your App.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="interval" value="00:10:00" />
</appSettings>
</configuration>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With