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

Read Windows service Config file

P: n/a
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
--
PeterW
Jun 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hello, PeterW!

P> Any tips please

Why don't you put it in the same place where, your service executable is placed?
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Jun 12 '06 #2

P: n/a
Hi Peter,

Thanks for your post!

ConfigurationSettings.AppSettings will only read current application's
config file, which is internally obtained by
AppDomain.CurrentDomain.SetupInformation.Configura tionFile 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("MySour ce"))
{
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(ServicesToRu n);
}

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

str=System.Configuration.ConfigurationSettings.App Settings["abc"].ToString()
;
}
else
{
str="null config";
}
eventLog1.WriteEntry(str);
str=AppDomain.CurrentDomain.SetupInformation.Confi gurationFile;
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.

Jun 13 '06 #3

P: n/a
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
--
PeterW
""Jeffrey Tan[MSFT]"" wrote:
Hi Peter,

Thanks for your post!

ConfigurationSettings.AppSettings will only read current application's
config file, which is internally obtained by
AppDomain.CurrentDomain.SetupInformation.Configura tionFile 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("MySour ce"))
{
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(ServicesToRu n);
}

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

str=System.Configuration.ConfigurationSettings.App Settings["abc"].ToString()
;
}
else
{
str="null config";
}
eventLog1.WriteEntry(str);
str=AppDomain.CurrentDomain.SetupInformation.Confi gurationFile;
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.

Jun 16 '06 #4

P: n/a
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.

Jun 16 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.