473,241 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Read Windows service Config file

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
4 22476
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marina | last post by:
Hi, I need to have a windows service that can access various DLL's dynamically. These dll's are stored in a particular directory, but of course this can vary by application. The problem is...
1
by: Troy Murphy | last post by:
I seem to remember using a walkthru in the early beta that demonstrated how to create a Windows Service that would monitor events in a folder. Can someone please direct me to an example of doing...
8
by: Alan Silver | last post by:
Hello, I have a need to read the web.config file from another web site. I am trying get the value of the "theme" attribute of the "pages" node. I initially tried to do this by loading the file...
1
by: John | last post by:
Hi, Trying to troubleshoot a web app. I'm getting the usual "set your web.config file to customErrors mode="Off"" message. Anyway, I DO have a web.config file in the root of the app, but it's...
2
by: freezerstud | last post by:
I have developed a windows control that I am using in an ASP.NET application. When I go to use the ConfigurationSettings.AppSettings function inside of the windows control nothing is returned. Is...
14
by: Peter | last post by:
..NET 3.5 I have a Windows Service application and it does remoting, but when a client incounters an error the client get the following error message "Server encountered an internal error....
4
by: pedro | last post by:
application cannot read the app.config file i have a config file for the executable i try to read it but it throws an exception when building the setup for the project vs2005 do not add the...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.