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!

What do you have to say?

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