# (easy?): finding directory

This topic is 4720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm using C#, and I want to know how to find the parent directory of wherever my executable is. In case I am getting my terminology wrong, I mean whatever folder contains my executable. I have looked on MSDN and on Google, but neither place turns anything up. [sad] I think in C++ I was able to omit the directory, but the System.IO.Directory.GetFiles() method needs the full path. [Edited by - Daniel Miller on June 21, 2005 1:06:10 PM]

##### Share on other sites
Maybe there is a different method that doesn't need the fully qualified path? I am stumped!

##### Share on other sites
See if Application.StartupPath() does what you want.

##### Share on other sites
That was it! [smile]

Thanks a lot, I don't know how I would have found that.

That is in windows forms, but this is a console application without access to windows forms. Is that the only way?

##### Share on other sites
OK, I just searched on the MSDN user groups and found that System.Environment.CurrentDirectory was it.

##### Share on other sites
System.Environment.CurrentDirectory is NOT what you want. The current directory is the directory where the application is launched. A application should never assume that its executable is in the current directory.

For example if an application is launched from the command line like this:
the System.Environment.CurrentDirectory will be C:\ and the Application.StartupPath will be C:\Windows The same problem arises when shortcuts are used; they can have the current directory set to another directory than where the executable is.

It is possible to use Application.StartupPath in a console application too, if you reference System.Windows.Forms.dll

##### Share on other sites
This is actually quite simple:

System.IO.FileInfo fi = new FileInfo(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);			return fi.Directory.FullName;

• 39
• 15
• 9
• 23
• 10