Settings for a windows service: where to store ?


G

Guest

From a .net windows service, I was tryng to access the app.config file using
System.Configuration.ConfigurationSettings.AppSettings, but it didn't return
the settings.

Can you access the app.config file from a .net windows service? If you can,
where does the file need to be? If you can't, where is a good place to store
data (connection strings, file paths etc) that you would normally put in the
app.config file?

Thanks,
Craig
 
Ad

Advertisements

R

Rob Schieber

Craig said:
From a .net windows service, I was tryng to access the app.config file using
System.Configuration.ConfigurationSettings.AppSettings, but it didn't return
the settings.

Can you access the app.config file from a .net windows service? If you can,
where does the file need to be? If you can't, where is a good place to store
data (connection strings, file paths etc) that you would normally put in the
app.config file?

Thanks,
Craig
Craig

Try the placing your app.config file in your system directory e.g.
C:\Windows\System32\ .
 
J

José Joye

By default the working directory is C:\Windows\System32\.
So what I will do is to redifine the working directory at startup of the
service

protected override void OnStart(string[] args)
{
try
{
// Define working directory (For a service, this is set to System)
Process pc = Process.GetCurrentProcess();
Directory.SetCurrentDirectory
(pc.MainModule.FileName.Substring(0,pc.MainModule.FileName.LastIndexOf(@"\")
));
....

This will allow you to them place the exe.config file in the same directory
of the service file and all is ok


- José
 
G

Guest

For any app, including windows services, the config file should be in the
same folder the assembly is located. and the service assembly can reside
anywhere you want, as long as you dont move it after its registered as a
service.

make sure your config file is named correctly.

appname.exe.config

so if your service is called myService.exe, then the config file will be
myService.exe.config and it should be in the same folder as the actual
executable.
 
Ad

Advertisements


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