473,508 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 22506
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
287
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
10041
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
3881
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
1427
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
1223
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
5560
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
4057
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
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.