Solved

Getting A Windows Service To Read Its Own COnfig File Not Machine.Config

Posted on 2008-06-23
13
889 Views
Last Modified: 2013-11-29
I need to read my own config file here is my code which fails with either a invalid exepath - or it just finds no appsettings.  LearnedOne - I thought I should post the question apart from my first. I have given you the points for the help on my first question.

Here is my code -  this is a windows service that I am debugging via a fom

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="DatabaseUpdaterRemotePath" value="Y:\IPB\Dch" />
    <add key="DatabaseUpdaterLocalPath" value="C:\IPXML\Dch" />
    <add key="DatabaseUpdaterFilter" value="*.XML*" />
    <add key="DatabaseUpdaterIncludeSubs" value="true" />
    <add key="DatabaseName" value="Disconnected.MDB"/>
  </appSettings>
</configuration>

onfiguration config = ConfigurationManager.OpenExeConfiguration(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase);

and this
//for testing my service
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath)

Then
RemotePath = config.AppSettings["DatabaseUpdaterRemotePath"].ToString();

this blows up and the appsettings are empty - it is automatically pulling from machine.config
0
Comment
Question by:DylanJones1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 

Author Comment

by:DylanJones1
ID: 21850221
Is reading a config file from a windows service that uncommon?
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 21853655
1) If your app is a Windows Service, it will expect its Config file to be in %WINDIR%\system32. So make sure that during or after an install, you copy the config file to that directory, your service should run fine.
2) //Set current directory to assembly folder
Environment.CurrentDirectory = System.IO.Path.GetDirectoryName
(System.Reflection.Assembly.GetEntryAssembly().Location);

at the start of my service
0
 

Author Comment

by:DylanJones1
ID: 21856086
Dhaest,

Thanks for your response.  I am not certain how to work in the line you proviided.  But... your code returns the debug path  \debug  which is what I expect and also what is returned by

System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase

and what the file path property is set to on my config object when I run

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
or
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);

All three approaches return the same path ...\debug but for all three the hasFile property is false and the file name that they point to are:

myconfig.exe.config    which does not exit.  

And this line with reference to the newly created config  

config.AppSettings["RemotePath"].ToString();

Will not even compile.


this is the code that errors

 Configuration config = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.ExecutablePath);
           
 //config.AppSettings["RemotePath"].ToString();









from above.

Then when I run the

0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:DylanJones1
ID: 21856255
Sorry those last two lines are should have been deleted.
0
 

Author Comment

by:DylanJones1
ID: 21857522
Anybody?  Do I have to change the build or output properties of the config file?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 21857710
Did you compile your project as Release ?
0
 

Author Comment

by:DylanJones1
ID: 21858447
I have bult for both and my config file sit in the bin\debug folder
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 21858475
Did you install the application ? When you test it in visual studio, it's normal that it goes to the bin\debug folder because the running exe is there !
0
 

Author Comment

by:DylanJones1
ID: 21859111
Right.  I am debugging  I am testing my service  using a test harness  while in debug mode  
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 21860004
You'll need to create an installer, install the application on a testserver (or other pc) and test it if it reads your config-file from the right directory (%WINDIR%\system32)
0
 

Author Comment

by:DylanJones1
ID: 21860014
I'll give it a go. Thanks
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 21862945
Did you try it ?
0
 

Author Comment

by:DylanJones1
ID: 21863306
Yes  It didn't work.  But as soon as I right clicked and said Add New Item chose application config file copy and pasted my config contents to it and removed the other config from my project it worked like a charm.   Odd....
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question