Read Windows service Config file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a Windows service that needs to get some values from a config file. I
place the config file for the service in the System32 directory. I do not
get the values using the usual ConfigurationSettings.AppSettings["keyname"]

Any tips please

cheers
 
Hi Peter,

Thanks for your post!

ConfigurationSettings.AppSettings will only read current application's
config file, which is internally obtained by
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile property. So you
should place the config file with name "appname.exe.config" in the same
directory with your service exe file.
Note: replace "appname" with your service exe name without the extension.

I have written a little sample Windows Service application for testing, it
works well with a local config file:

//MyNewService.exe.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="abc" value="Hello World" />
</appSettings>
</configuration>

//In MyNewService.cs file

System.Diagnostics.EventLog eventLog1=null;
public MyNewService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
eventLog1=new EventLog();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource","MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";

// TODO: Add any initialization after the InitComponent call
}

static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
MyNewService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

protected override void OnStart(string[] args)
{
string str;
if(System.Configuration.ConfigurationSettings.AppSettings["abc"]!=null)
{

str=System.Configuration.ConfigurationSettings.AppSettings["abc"].ToString()
;
}
else
{
str="null config";
}
eventLog1.WriteEntry(str);
str=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
eventLog1.WriteEntry(str);

// TODO: Add code here to start your service.
}

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thank you very much. Just what the doctor ordered. There were a few other
minor problems that were clouding issues but all is now fine.

cheers
 
I am glad you finally got it work. If you need further help, please feel
free to post. Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top