Jump to content
  • Advertisement
Sign in to follow this  
Daniel Miller

(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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
That was it! [smile]

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

edit: NOO!! [sad]

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

Share this post


Link to post
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:
C:\> Windows\notepad.exe
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 this post


Link to post
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;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!