10

I am developing a windows service, and I am almost done,

I am doing that using Visual Studio 2012.

I am connection to the App.config file in order to read the connection string because the windows service deals with data installed in a database.

I am able to install this Windows service on my machine, but I am asking if this service will still be able to read from the App.config after being installed? or I must hard coded the connection string?

I hope I was able to explain my problem. However, If you dont' understand me, please tell me to inform you more.

Best regard,

Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • Related: http://stackoverflow.com/questions/14074563/windows-service-app-config-location – Habib Oct 22 '14 at 19:02

2 Answers2

22

Yes you will be able. When you will compile your service, you will need to deploy the whole /bin directory. It will contains the service dependencies and actual code.

Once compiled, the App.config file will be renamed to: Service.exe.config. The content will be a copy of the App.config. This is the file that will be used by the service. Service.exe is the name of your executable. So if your executable is AwesomeService.exe the config file will be AwesomeService.exe.config.

Charles Ouellet
  • 6,338
  • 3
  • 41
  • 57
  • I used to register the windows service by using `installutil myservice.exe`, but how can I register the whole `bin` folder please? – Marco Dinatsoli Oct 22 '14 at 19:30
  • 1
    I usually use `sc create` http://stackoverflow.com/questions/3663331/creating-a-service-with-sc-exe-how-to-pass-in-context-parameters I copy the content of the bin folder to the server I want to deploy to (when it is not automated), it contains the `.exe` of your service and the `.dll` that your service depends on as long as the `.config` file. – Charles Ouellet Oct 22 '14 at 19:32
2

An even simpler approach would be to read the specific config file according to a specific path! Here's a working sample:

System.Configuration.Configuration conf = System.Configuration.ConfigurationManager.OpenExeConfiguration("<full path including component name.exe (and not including the '.config' part");
string PropValue = conf.AppSettings.Settings["PropertName"].Value;
nivhab
  • 677
  • 6
  • 12