Current Directory For Windows Service Is Not What You Expect

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.


System.IO.Directory.SetCurrentDirectory
(System.AppDomain.CurrentDomain.BaseDirectory);

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.

What others have said

Requesting Gravatar... Web Development Mar 15, 2005 6:33 AM
# re: Current Directory For Windows Service Is Not What You Expect
Good point and a lot of thanks.
Requesting Gravatar... Neill May 10, 2006 11:07 AM
# re: Current Directory For Windows Service Is Not What You Expect
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.
Requesting Gravatar... Mike Oct 18, 2007 11:21 AM
# re: Current Directory For Windows Service Is Not What You Expect
Thank you! I love the internet.
Requesting Gravatar... Pierre Mengal Dec 12, 2007 6:32 AM
# re: Current Directory For Windows Service Is Not What You Expect
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.
Requesting Gravatar... Maciej Mar 12, 2009 8:11 AM
# re: Current Directory For Windows Service Is Not What You Expect
Great stuff mate, helped me a lot, huge cheers for that!
Requesting Gravatar... Maik Jan 05, 2010 11:16 AM
# re: Current Directory For Windows Service Is Not What You Expect
Thanks!

In my program I had to put this before saving a file because it keeps asking for the last drive ( diskette in my case )
Requesting Gravatar... Lori Jun 25, 2010 2:35 PM
# re: Current Directory For Windows Service Is Not What You Expect
Oh god, I've ran into it when I tried to configure log4net in a windows service.. thx.
Requesting Gravatar... Shoel Aug 05, 2010 2:43 PM
# re: Current Directory For Windows Service Is Not What You Expect
thanks a ton!
Requesting Gravatar... Yuri Aug 10, 2010 5:31 AM
# re: Current Directory For Windows Service Is Not What You Expect
And if you run a 32 bits windows service on windows 64 bits the directory is: C:\Windows\SysWOW64

What do you have to say?

(will show your gravatar)
Please add 4 and 5 and type the answer here: