By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,020 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,020 IT Pros & Developers. It's quick & easy.

Reading configuration settings from a Windows Service app

P: n/a
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\ServiceName 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
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

Any replies to this would be greatly appreciated.

Thanks
Dhilip Kumar

"Dhilip Kumar" <dd*****@gmail.com> wrote in message
news:%2****************@tk2msftngp13.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\ServiceName 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

P: n/a
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****************@tk2msftngp13.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\ServiceName 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

P: n/a
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.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call

try
{
this.obmPollTime =
this.serverRegistryHKLM.GetValue("PollTime").ToStr ing();
}
catch (Exception ex)
{
EventLog.WriteEntry("Exception occurred while trying to read polltime.
Error: " + ex.Message, EventLogEntryType.Error);
this.Onstop();
}
}

private void InitializeComponent()
{
this.performanceCounter1 = new
System.Diagnostics.PerformanceCounter("System","Pr ocesses", "");
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.p erformanceCounter1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.t imer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = false;
this.timer1.Interval = 10000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elap sed);
//
// OBMServerTry
//
this.ServiceName = "OBMServerTry";
((System.ComponentModel.ISupportInitialize)(this.p erformanceCounter1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.t imer1)).EndInit();

//the following line is manually added
this.serverRegistryHKLM =
Registry.LocalMachine.OpenSubKey("SOFTWARE\\CBM\\O BM");

}

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.machin AT dot.state.fl.us> wrote
in message news:Oy*************@TK2MSFTNGP10.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****************@tk2msftngp13.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\ServiceName 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

P: n/a
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****************@TK2MSFTNGP15.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.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call

try
{
this.obmPollTime =
this.serverRegistryHKLM.GetValue("PollTime").ToStr ing();
}
catch (Exception ex)
{
EventLog.WriteEntry("Exception occurred while trying to read polltime.
Error: " + ex.Message, EventLogEntryType.Error);
this.Onstop();
}
}

private void InitializeComponent()
{
this.performanceCounter1 = new
System.Diagnostics.PerformanceCounter("System","Pr ocesses", "");
this.timer1 = new System.Timers.Timer();

((System.ComponentModel.ISupportInitialize)(this.p erformanceCounter1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.t imer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = false;
this.timer1.Interval = 10000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elap sed);
//
// OBMServerTry
//
this.ServiceName = "OBMServerTry";

((System.ComponentModel.ISupportInitialize)(this.p erformanceCounter1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.t imer1)).EndInit();

//the following line is manually added
this.serverRegistryHKLM =
Registry.LocalMachine.OpenSubKey("SOFTWARE\\CBM\\O BM");

}

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.machin AT dot.state.fl.us>
wrote in message news:Oy*************@TK2MSFTNGP10.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****************@tk2msftngp13.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\ServiceName 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

P: n/a
Thanks Ignacio!

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Oy***************@TK2MSFTNGP12.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****************@TK2MSFTNGP15.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.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call

try
{
this.obmPollTime =
this.serverRegistryHKLM.GetValue("PollTime").ToStr ing();
}
catch (Exception ex)
{
EventLog.WriteEntry("Exception occurred while trying to read polltime.
Error: " + ex.Message, EventLogEntryType.Error);
this.Onstop();
}
}

private void InitializeComponent()
{
this.performanceCounter1 = new
System.Diagnostics.PerformanceCounter("System","Pr ocesses", "");
this.timer1 = new System.Timers.Timer();

((System.ComponentModel.ISupportInitialize)(this.p erformanceCounter1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.t imer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = false;
this.timer1.Interval = 10000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elap sed);
//
// OBMServerTry
//
this.ServiceName = "OBMServerTry";

((System.ComponentModel.ISupportInitialize)(this.p erformanceCounter1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.t imer1)).EndInit();

//the following line is manually added
this.serverRegistryHKLM =
Registry.LocalMachine.OpenSubKey("SOFTWARE\\CBM\\O BM");

}

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.machin AT dot.state.fl.us>
wrote in message news:Oy*************@TK2MSFTNGP10.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****************@tk2msftngp13.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\ServiceName 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 discussion thread is closed

Replies have been disabled for this discussion.