Windows service with a configuration file

N

noah.blumenthal

I wrote [my first] a windows service in c# (applause) and now I want to
add the ability to edit its settings. Basically this service checks an
email account at certain intervals and forwards the emails to another
email address. I want to be able to configure how often it runs (it
uses a timer, so setting the interval for that timer is what I mean by
that statement) and what account to check and what email address to
send to.

Seems easy, right? Well, I created a Settings.settings file and
populated it with all my variables (incomingPopServer,
incomingPopUsername, incomingPopPassword, etc...) that the service sets
onStart, but then I realized that I don't know how to get a form to
read & write these values.

I figured the way to do this is to create another VS.NET Project (the
service was called "Emailer Service" so this would be called "Emailer")
and create a single form with a few textboxes and a "Save" button that
saves those values to the Email Service's Settings.settings and then
restarts the service using the ServiceController class.

And here come my questions for you:

1) How do I get this Emailer project to "see" the settings from the
Emailer Service and how, once it sees them, do I get it to write to
them.
2) Is this even the best way to go about it? Might there be a
better/smarter/cheaper way of doing this?

Did that make any sense?

Thank you in advance,
Noah
 
S

Samuel R. Neff

Yes you want a separate application. Typically a windows service with
a UI will have one app which is the windows service itself and a
totally separate app which is the UI. The service runs whether or not
the UI is running.

For your situation, you don't need the two apps to actually talk to
each other, they just need to share the same config file. Don't use
the Setings stuff--that's for preset application configuration and not
user configurable data. Instead use a custom file, most likely XML,
and put it in a standard location that both can find (suggest
Environment.SpecialFolders.CommonApplicationData as the root). You'll
want to have a dll that both apps share in common for reading/writing
the XML file in order to not have duplicated code.

When the UI changes the config file, you can use ServiceController to
restart the service from the UI app. Or you can use a
FileSystemWatcher in the service to watch the config file and
auto-reload when it changes (be sure to have a little delay to be sure
the UI is done writing changes before you reload).

HTH,

Sam


------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.


I wrote [my first] a windows service in c# (applause) and now I want to
add the ability to edit its settings. Basically this service checks an
email account at certain intervals and forwards the emails to another
email address. I want to be able to configure how often it runs (it
uses a timer, so setting the interval for that timer is what I mean by
that statement) and what account to check and what email address to
send to.

Seems easy, right? Well, I created a Settings.settings file and
populated it with all my variables (incomingPopServer,
incomingPopUsername, incomingPopPassword, etc...) that the service sets
onStart, but then I realized that I don't know how to get a form to
read & write these values.

I figured the way to do this is to create another VS.NET Project (the
service was called "Emailer Service" so this would be called "Emailer")
and create a single form with a few textboxes and a "Save" button that
saves those values to the Email Service's Settings.settings and then
restarts the service using the ServiceController class.

And here come my questions for you:

1) How do I get this Emailer project to "see" the settings from the
Emailer Service and how, once it sees them, do I get it to write to
them.
2) Is this even the best way to go about it? Might there be a
better/smarter/cheaper way of doing this?

Did that make any sense?

Thank you in advance,
Noah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top