Current directory

J

Jon Rea

Hi,

Just a quickie ...

I thought that Directory.GetCurrentGirectory(), unless the current directory
had been changed by the program, would always point to the directory where
the .exe for the program was. However, if my program is launched from the
start menu in windows XP, Directory.GetCurrentGirectory() returns a special
windows directory for the user in "Documents and Settings" under c:\. As i
want to open files that are in the directory of the exe file, how do i get
the folder that holds the exe?

Thanks
Jon
 
G

Guest

Hi,

Have you tried FullyQualifiedName?

this.mStackFrame = new System.Diagnostics.StackFrame( 2, true );
System.Type type = this.mStackFrame.GetMethod().DeclaringType;
this.mstrCompiledFileLocation = type.Module.FullyQualifiedName;

HTH
 
C

Chris

using the current directory doesn't always work since it can be changed
arbitrarily by other programs. I first get the assembly path (which
includes the .exe file name).

string path = System.Reflection.GetCallingAssembly().Location;

then you can take out the file name on the end if you don't want it.

another way to get it is using this:

stirng path = System.Windows.Forms.Application.StartupPath;

Chris
 
J

John Wood

It's probably better to use GetEntryAssembly() rather than
GetCallingAssembly() (as that would normally give you the entry-point
executable).

You can use the Path class to split up the path from the filename.
 
J

Jon Skeet [C# MVP]

Chris said:
using the current directory doesn't always work since it can be changed
arbitrarily by other programs.

Worse than that - it gets changed as soon as you ask the user for a
file/folder, I believe...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top