Wednesday, April 24, 2013

Windows service and relative file path problem

I created a console application which was dependent of an xml file, I set the property of file to always copy and I always for new version of xml file inside execution directory and my console application worked until one day I wrapped that console application into windows service to run it periodically. And then this problem started.


When I run it in debug mode or console app mode it worked. I can to know about this error when I put a logging statement to log the error in eventviewer and then I found that Windows service code was looking for this file under system folder, here is error message I got in log “Could not find file 'C:\Windows\system32\ticks.xml'.”  


Actually windows service runs from within system folder and their current directory (Environment.CurrentDirectory) is set to system directory and hence any relative lookup is based on this path.


To fix this problem, build your file path using AppDomain.CurrentDomain.BaseDirectory and relative path.





1 comment:

Anonymous said...

This Solution resoved my issue of windows service