C # Windows Service is (possibly) using app.config

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

I am doing this using Visual Studio 2012.

I am connecting to a file App.config

to read the connection string because the windows service deals with the data set in the database.

I can install this Windows service on my machine, but I am asking if this service can still read from App.config

after installing it? or should I hardcode the connection string?

Hope I was able to explain my problem. However, if you do not understand me, please tell me to let you know more.

Respectfully,

+3


source to share


2 answers


Yes you can. When you compile your service, you will need to expand the entire directory /bin

. It will contain the service dependencies and the actual code.



After compilation, the file App.config

will be renamed to: Service.exe.config

. The content will be a copy App.config

. This is the file that will be used by the service. Service.exe

is the name of the executable file. Therefore, if your executable file AwesomeService.exe

, the config file will be AwesomeService.exe.config

.

+13


source


An even simpler approach is to read a 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;

      

+1


source







All Articles