473,719 Members | 2,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading configuration settings from a Windows Service app

Hi All,

I'm writing a Windows Service app using C#. I need to read some
configuration settings before the service starts up. These settings will be
used by the service in its operation. Question is, which is the best way to
store & retrieve the settings? I'm thinking of storing it in the registry
in HKLM\Software\S erviceName and access it using the Registry class in the
"public ServiceName()" method. I'm instructing the service installer to
use "Local Service" account since "Local System" has too many priveleges
which is risky. Would accessing the registry for start-up settings be fine,
or is there any risk associated with doing so? The other alternative is to
use a App.config file I guess. Which one is better? And what are the
possible risks in using the registry from a service?

Thanks,
Dhilip Kumar
Nov 16 '05 #1
5 12935
Hi,

Any replies to this would be greatly appreciated.

Thanks
Dhilip Kumar

"Dhilip Kumar" <dd*****@gmail. com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi All,

I'm writing a Windows Service app using C#. I need to read some
configuration settings before the service starts up. These settings will
be used by the service in its operation. Question is, which is the best
way to store & retrieve the settings? I'm thinking of storing it in the
registry in HKLM\Software\S erviceName and access it using the Registry
class in the "public ServiceName()" method. I'm instructing the service
installer to use "Local Service" account since "Local System" has too many
priveleges which is risky. Would accessing the registry for start-up
settings be fine, or is there any risk associated with doing so? The
other alternative is to use a App.config file I guess. Which one is
better? And what are the possible risks in using the registry from a
service?

Thanks,
Dhilip Kumar

Nov 16 '05 #2
Hi,

If you only require read capabilities then use the app.config , it's local
to your app and .net gives you easy access to it in a transparent way.

You could also use the registry, there is nothing wrong doing so, but I
would keep there only those values that you need to change from your code
( app.config is ready only )

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Dhilip Kumar" <dd*****@gmail. com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi All,

I'm writing a Windows Service app using C#. I need to read some
configuration settings before the service starts up. These settings will
be used by the service in its operation. Question is, which is the best
way to store & retrieve the settings? I'm thinking of storing it in the
registry in HKLM\Software\S erviceName and access it using the Registry
class in the "public ServiceName()" method. I'm instructing the service
installer to use "Local Service" account since "Local System" has too many
priveleges which is risky. Would accessing the registry for start-up
settings be fine, or is there any risk associated with doing so? The
other alternative is to use a App.config file I guess. Which one is
better? And what are the possible risks in using the registry from a
service?

Thanks,
Dhilip Kumar

Nov 16 '05 #3
Hi Ignacio,

Thanks for your reply. Yes some of these values could be changed by the
end-user if needed. Hence the need to keep it in the registry.

I have one more question. While retrieving values from registry, I need to
make sure that some of the registry keys are present and have values. If
not I need to stop loading (starting the service). I tried to trigger the
OnStop method as follows but it doesn't help:

public OBMServerTry()
{
// This call is required by the Windows.Forms Component Designer.
InitializeCompo nent();

// TODO: Add any initialization after the InitComponent call

try
{
this.obmPollTim e =
this.serverRegi stryHKLM.GetVal ue("PollTime"). ToString();
}
catch (Exception ex)
{
EventLog.WriteE ntry("Exception occurred while trying to read polltime.
Error: " + ex.Message, EventLogEntryTy pe.Error);
this.Onstop();
}
}

private void InitializeCompo nent()
{
this.performanc eCounter1 = new
System.Diagnost ics.Performance Counter("System ","Processe s", "");
this.timer1 = new System.Timers.T imer();
((System.Compon entModel.ISuppo rtInitialize)(t his.performance Counter1)).Begi nInit();
((System.Compon entModel.ISuppo rtInitialize)(t his.timer1)).Be ginInit();
//
// timer1
//
this.timer1.Ena bled = false;
this.timer1.Int erval = 10000;
this.timer1.Ela psed += new
System.Timers.E lapsedEventHand ler(this.timer1 _Elapsed);
//
// OBMServerTry
//
this.ServiceNam e = "OBMServerT ry";
((System.Compon entModel.ISuppo rtInitialize)(t his.performance Counter1)).EndI nit();
((System.Compon entModel.ISuppo rtInitialize)(t his.timer1)).En dInit();

//the following line is manually added
this.serverRegi stryHKLM =
Registry.LocalM achine.OpenSubK ey("SOFTWARE\\C BM\\OBM");

}

What happens is that the try catch block is executed and an exception is
caught, but the service proceeds to start though I have triggered OnStop()!!
My guess at why the service starts is that the Windows Service project is
designed to receive commands from the SCM and hence it ignores the OnStop()
method triggered by the service code itself. Or am I missing something in
the code? I am just trying to find out how to stop loading the service if
any of the required registry parameters are missing and send an appropriate
response back to the SCM!

Thanks
Dhilip Kumar

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:Oy******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

If you only require read capabilities then use the app.config , it's local
to your app and .net gives you easy access to it in a transparent way.

You could also use the registry, there is nothing wrong doing so, but I
would keep there only those values that you need to change from your code
( app.config is ready only )

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Dhilip Kumar" <dd*****@gmail. com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi All,

I'm writing a Windows Service app using C#. I need to read some
configuration settings before the service starts up. These settings will
be used by the service in its operation. Question is, which is the best
way to store & retrieve the settings? I'm thinking of storing it in the
registry in HKLM\Software\S erviceName and access it using the Registry
class in the "public ServiceName()" method. I'm instructing the
service installer to use "Local Service" account since "Local System" has
too many priveleges which is risky. Would accessing the registry for
start-up settings be fine, or is there any risk associated with doing so?
The other alternative is to use a App.config file I guess. Which one is
better? And what are the possible risks in using the registry from a
service?

Thanks,
Dhilip Kumar


Nov 16 '05 #4
Hi,

For the changing values use the registry.

You can check it in the OnStart method if not found throw an exception IIRC
this will be logged in the event log under application.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Dhilip Kumar" <dd*****@gmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Hi Ignacio,

Thanks for your reply. Yes some of these values could be changed by the
end-user if needed. Hence the need to keep it in the registry.

I have one more question. While retrieving values from registry, I need
to make sure that some of the registry keys are present and have values.
If not I need to stop loading (starting the service). I tried to trigger
the OnStop method as follows but it doesn't help:

public OBMServerTry()
{
// This call is required by the Windows.Forms Component Designer.
InitializeCompo nent();

// TODO: Add any initialization after the InitComponent call

try
{
this.obmPollTim e =
this.serverRegi stryHKLM.GetVal ue("PollTime"). ToString();
}
catch (Exception ex)
{
EventLog.WriteE ntry("Exception occurred while trying to read polltime.
Error: " + ex.Message, EventLogEntryTy pe.Error);
this.Onstop();
}
}

private void InitializeCompo nent()
{
this.performanc eCounter1 = new
System.Diagnost ics.Performance Counter("System ","Processe s", "");
this.timer1 = new System.Timers.T imer();

((System.Compon entModel.ISuppo rtInitialize)(t his.performance Counter1)).Begi nInit();
((System.Compon entModel.ISuppo rtInitialize)(t his.timer1)).Be ginInit();
//
// timer1
//
this.timer1.Ena bled = false;
this.timer1.Int erval = 10000;
this.timer1.Ela psed += new
System.Timers.E lapsedEventHand ler(this.timer1 _Elapsed);
//
// OBMServerTry
//
this.ServiceNam e = "OBMServerT ry";

((System.Compon entModel.ISuppo rtInitialize)(t his.performance Counter1)).EndI nit();
((System.Compon entModel.ISuppo rtInitialize)(t his.timer1)).En dInit();

//the following line is manually added
this.serverRegi stryHKLM =
Registry.LocalM achine.OpenSubK ey("SOFTWARE\\C BM\\OBM");

}

What happens is that the try catch block is executed and an exception is
caught, but the service proceeds to start though I have triggered
OnStop()!! My guess at why the service starts is that the Windows Service
project is designed to receive commands from the SCM and hence it ignores
the OnStop() method triggered by the service code itself. Or am I missing
something in the code? I am just trying to find out how to stop loading
the service if any of the required registry parameters are missing and
send an appropriate response back to the SCM!

Thanks
Dhilip Kumar

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
wrote in message news:Oy******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

If you only require read capabilities then use the app.config , it's
local to your app and .net gives you easy access to it in a transparent
way.

You could also use the registry, there is nothing wrong doing so, but I
would keep there only those values that you need to change from your code
( app.config is ready only )

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Dhilip Kumar" <dd*****@gmail. com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi All,

I'm writing a Windows Service app using C#. I need to read some
configuration settings before the service starts up. These settings
will be used by the service in its operation. Question is, which is the
best way to store & retrieve the settings? I'm thinking of storing it
in the registry in HKLM\Software\S erviceName and access it using the
Registry class in the "public ServiceName()" method. I'm instructing
the service installer to use "Local Service" account since "Local
System" has too many priveleges which is risky. Would accessing the
registry for start-up settings be fine, or is there any risk associated
with doing so? The other alternative is to use a App.config file I
guess. Which one is better? And what are the possible risks in using
the registry from a service?

Thanks,
Dhilip Kumar



Nov 16 '05 #5
Thanks Ignacio!

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:Oy******** *******@TK2MSFT NGP12.phx.gbl.. .
Hi,

For the changing values use the registry.

You can check it in the OnStart method if not found throw an exception
IIRC this will be logged in the event log under application.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Dhilip Kumar" <dd*****@gmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Hi Ignacio,

Thanks for your reply. Yes some of these values could be changed by the
end-user if needed. Hence the need to keep it in the registry.

I have one more question. While retrieving values from registry, I need
to make sure that some of the registry keys are present and have values.
If not I need to stop loading (starting the service). I tried to trigger
the OnStop method as follows but it doesn't help:

public OBMServerTry()
{
// This call is required by the Windows.Forms Component Designer.
InitializeCompo nent();

// TODO: Add any initialization after the InitComponent call

try
{
this.obmPollTim e =
this.serverRegi stryHKLM.GetVal ue("PollTime"). ToString();
}
catch (Exception ex)
{
EventLog.WriteE ntry("Exception occurred while trying to read polltime.
Error: " + ex.Message, EventLogEntryTy pe.Error);
this.Onstop();
}
}

private void InitializeCompo nent()
{
this.performanc eCounter1 = new
System.Diagnost ics.Performance Counter("System ","Processe s", "");
this.timer1 = new System.Timers.T imer();

((System.Compon entModel.ISuppo rtInitialize)(t his.performance Counter1)).Begi nInit();
((System.Compon entModel.ISuppo rtInitialize)(t his.timer1)).Be ginInit();
//
// timer1
//
this.timer1.Ena bled = false;
this.timer1.Int erval = 10000;
this.timer1.Ela psed += new
System.Timers.E lapsedEventHand ler(this.timer1 _Elapsed);
//
// OBMServerTry
//
this.ServiceNam e = "OBMServerT ry";

((System.Compon entModel.ISuppo rtInitialize)(t his.performance Counter1)).EndI nit();
((System.Compon entModel.ISuppo rtInitialize)(t his.timer1)).En dInit();

//the following line is manually added
this.serverRegi stryHKLM =
Registry.LocalM achine.OpenSubK ey("SOFTWARE\\C BM\\OBM");

}

What happens is that the try catch block is executed and an exception is
caught, but the service proceeds to start though I have triggered
OnStop()!! My guess at why the service starts is that the Windows Service
project is designed to receive commands from the SCM and hence it ignores
the OnStop() method triggered by the service code itself. Or am I
missing something in the code? I am just trying to find out how to stop
loading the service if any of the required registry parameters are
missing and send an appropriate response back to the SCM!

Thanks
Dhilip Kumar

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
wrote in message news:Oy******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

If you only require read capabilities then use the app.config , it's
local to your app and .net gives you easy access to it in a transparent
way.

You could also use the registry, there is nothing wrong doing so, but I
would keep there only those values that you need to change from your
code ( app.config is ready only )

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Dhilip Kumar" <dd*****@gmail. com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi All,

I'm writing a Windows Service app using C#. I need to read some
configuration settings before the service starts up. These settings
will be used by the service in its operation. Question is, which is
the best way to store & retrieve the settings? I'm thinking of storing
it in the registry in HKLM\Software\S erviceName and access it using the
Registry class in the "public ServiceName()" method. I'm instructing
the service installer to use "Local Service" account since "Local
System" has too many priveleges which is risky. Would accessing the
registry for start-up settings be fine, or is there any risk associated
with doing so? The other alternative is to use a App.config file I
guess. Which one is better? And what are the possible risks in using
the registry from a service?

Thanks,
Dhilip Kumar



Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2880
by: Florida Coder | last post by:
I have the need to store some application specific configuration data to be used by a class library and or a windows service. I would like to do this in a fashion similar to the way we do with applications and web services without having to use the machine.config file. I know that I could use the code below to do this but the problem then becomes that when using a shared assembly both the assembly and the config file need to be put...
0
1209
by: Jorgas | last post by:
Hi! I try to use the configuration application block from the Enterprise Library and have done prework as specified from one of the hands-on labs from that package. When trying to get configuration settings using the ConfigurationManager.GetConfiguration my application, which is a windows service, stops with the following error message (parts of it) written to the event log. "Service cannot be started. System.IO.FileNotFoundException:...
1
1174
by: Philip Wagenaar | last post by:
Hi, I used the microsoft configuration block to store settings for applications in the past and this worked fine. But now I have the following situation and I do not know how to solve this: I want to write a windows service that stores its settings in a xml file using the configuration block. And I want to write a windows forms application that manages the service. Stop,start and modify the settings.
1
19030
by: Russ | last post by:
I have a windows service written in C#, and whenever I start it up, I get the error "Configuration System failed to initialize" when ever I call ConfigurationManager.AppSettings. I tried hard coding the values, but I also have a web service I am calling in this windows service, and as soon as the web service is initialized, I get the same error. This is my entire app.config Please help, I have spend like 2 hours on a service that...
7
22488
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We added an entry to the executable's .exe.config file to specify the URL, and under the 1.1 framework this worked well. Unfortunately, this is not working under the 2.0 framework. I see in the Reference.cs file under the web service reference the...
2
14255
by: Olegus | last post by:
I have Windows service MyService, that reads some settings from MyService.exe.config file via ConfigurationManager.AppSettings. I tried to write small WinForm frontend app(called it Manager.exe) to manage settings of MyService, but have problems with accessing MyService.exe.config from Manager app. I am getting this error: "System.Configuration.ConfigurationElement.this' is inaccessible due to its protection level"
7
10322
by: Peter Bradley | last post by:
OK. A bit behind the times, I know; but we're just moving over to .NET 2.0. How on earth do you manage configuration settings in a class library in .NET 2.0? In version 1.1, we used a handy class called AssemblySettings that someone (I forget his name) had written. When the class library was finished, you deployed it to the GAC and put the configuration files in the GAC with the class library assembly. This no longer works. In fact...
4
57094
by: aSoundMind | last post by:
Hi there, I recieve this error Server Error in '/integrate' Application. -------------------------------------------------------------------------------- Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
0
8184
by: Lucky | last post by:
Hi guys, I've got a problem under reading settlings from App.config file. I've used System.Configuration to reach to the "User Settings" and "Application Settings" sections. the problem is now I'm not able read the property defined under this section. those are like this User Settings -Testing Application Settings -Test
0
8833
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9126
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7998
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5972
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.