Current Directory For Windows Service Is Not What You Expect

0 comments suggest edit

At least it wasn’t what I expected.  By default, the current directory for your Windows service is the System32 folder.  I keep forgetting that which causes me problems when I try to access a file or folder using a relative path.


Use the above line of code to set the current directory to the same directory as your windows service. Don’t say I didn’t warn you. I shall never forget again.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.



29 responses

  1. Avatar for Web Development
    Web Development March 14th, 2005

    Good point and a lot of thanks.

  2. Avatar for Neill
    Neill May 10th, 2006

    Thank you for this great help. I first searched google for currentworkingdirectory, path, application directory, etc. and didn't get much help. I'm including those search terms here so others can land here as well.

  3. Avatar for Mike
    Mike October 18th, 2007

    Thank you! I love the internet.

  4. Avatar for Pierre Mengal
    Pierre Mengal December 11th, 2007

    If you want to get the executing directory, you can still use : string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
    I use the Phil's method to set it back to the process.

  5. Avatar for Maciej
    Maciej March 11th, 2009

    Great stuff mate, helped me a lot, huge cheers for that!

  6. Avatar for Maik
    Maik January 5th, 2010

    In my program I had to put this before saving a file because it keeps asking for the last drive ( diskette in my case )

  7. Avatar for Lori
    Lori June 25th, 2010

    Oh god, I've ran into it when I tried to configure log4net in a windows service.. thx.

  8. Avatar for Shoel
    Shoel August 5th, 2010

    thanks a ton!

  9. Avatar for Yuri
    Yuri August 9th, 2010

    And if you run a 32 bits windows service on windows 64 bits the directory is: C:\Windows\SysWOW64

  10. Avatar for Chris
    Chris April 19th, 2011

    Helped a lot .. thanks ..

  11. Avatar for Jon
    Jon February 21st, 2012

    Just ran into this when installing an msi with msiexec. Custom action logs went to the C:\Windows\SysWow64 folder.

  12. Avatar for Diego
    Diego February 28th, 2012

    Thanks a lot, just saved me from hours of madness! Old but gold tip!

  13. Avatar for Roel
    Roel April 13th, 2012

    Thanks a lot!

  14. Avatar for Misha
    Misha July 19th, 2012

    Like a boss!

  15. Avatar for Vjekoslav
    Vjekoslav January 9th, 2013

    Thanks a lot

  16. Avatar for Scoraig52
    Scoraig52 January 27th, 2013

    Good tip, I needed this one

  17. Avatar for Ba3
    Ba3 May 4th, 2013

    Excelente tip. You are the master!

  18. Avatar for Kikikahlua
    Kikikahlua June 24th, 2013

    OMG, I was stuck with this problem for 2 days!!!! Thank you sooooooo much!!! You saved my life!!!!

  19. Avatar for Leonid Morgenshtern
    Leonid Morgenshtern October 17th, 2013

    9 years and still relevant, thanks!

  20. Avatar for Dario G
    Dario G March 20th, 2014

    Just wanted to personally thank you for this information. I was incurring the dreaded Error: 1054 due to my application calling settings out of a conf file. Little did I realize, my application was trying to fetch this file from within the system32 folder, not the folder local to the service executable itself! At any rate, this simple one-liner addressed my startup issue, thank you kindly!

  21. Avatar for Rahul
    Rahul April 25th, 2014

    My service was writing to a file denoted by relative path and I was like, "Where the hell is the file! #$%#". I had an inkling this could be the issue but when I turned to Google and this turned up - Felt so good, man. Thanks :D

  22. Avatar for MrMikeJJ
    MrMikeJJ January 16th, 2015

    Thanks, i just got bitten by this as well.

  23. Avatar for harry66
    harry66 March 3rd, 2015

    Thanks - this was doing my head in!

  24. Avatar for joonas_fi
    joonas_fi April 15th, 2015

    Thanks alot, mate!

  25. Avatar for InverterOfControl
    InverterOfControl March 16th, 2016

    After all this time. This post saved me today as well!

  26. Avatar for Markeli
    Markeli November 13th, 2016


  27. Avatar for Massimo Giambona
    Massimo Giambona December 22nd, 2017


  28. Avatar for Diego
    Diego July 2nd, 2018

    Great point!! Thanks.

  29. Avatar for Sam
    Sam July 27th, 2018

    How to change that when we install a new exe as a Windows Service?