tags:

views:

1394

answers:

7

For my apps, I store some configuration file in xml along with the assembly(exe), and something other temporary files for proccessing purpose.

I found some quirk with ".\\" and Application.StartupPath.

I've been using

String configPath = ".\\config.xml";

It works fine until I called OpenFIleDialog to open some files in other folders, the statement above failed. Apparently ".\" is referring to "CurrentDirectory", which changes every time when we browse to another folder.

At some point, I was using

String configPath = Path.Combine(Application.StartupPath + "config.xml");

At some point, when I need to execute this assembly from another folder by using Process.Start(), things start to fall apart. Apparently the working directory is not set properly, and Application.StartupPath is actually referring to working directory instead of the directory of which the assembly is executing, as I've assumed. So I have to resort to using ProcessInfo to setup the working directory to the assembly's directory. I also had problem with this when I was writing VSTO.

So, my question is, what's the best, simplest and most assured way to get the current directory that the assembly is executing, without those quirks(or misunderstanding) that I've just mentioned?

EDIT: I meant to get the directory which the assembly reside

EDIT: According to MSDN on AppDomain.BaseDirectory, it seems that it can be changes during runtime, which is what I don't want(Just to clarify, not that I don't want to allow changing BaseDirectory, but rather, when I retrieve it without knowing for sure whether it's been changed)

EDIT: I've notice that a related question was posted much earlier. What would cause the current directory of an executing app to change?

Thanks guys for the answer.

+7  A: 

Do you want the actual working directory, or the directory containing the assembly? It's not entirely clear.

There's Environment.CurrentDirectory if you want the working directory, or Path.GetDirectoryName(typeof(Foo).Assembly.ManifestModule.FullyQualifiedName) to find the location of an assembly (or at least its manifest module, which will be the same thing in almost all cases).

Jon Skeet
+5  A: 

There is also,

System.Reflection.Assembly.GetExecutingAssembly().CodeBase
tafa
@tafa, Beat me to it. :) @Jon, Also try System.Reflection.Assembly.GetExecutingAssembly().Location
Kon
Both of which are not the answers that are asked for actually. Robinkor's is a more complete answer.
tafa
+1  A: 

How about

string currentAssemblyFile = System.Reflection.Assembly.GetExecutingAssembly().Location;

and then figure it out from there...

GalacticCowboy
+7  A: 
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location))
korro
does this method involved reflection? as in performance impact?
faulty
I'm not sure that there is a 1:1 correlation between "reflection" and "performance impact"; it's in the Reflection namespace but doesn't actually reflect into the assembly - it's using the metadata that it already knows.
GalacticCowboy
Yeah... Make only ONE CALL like this to see it suck all your CPU cycles and turn your Xeon Octa-core into a HP12c. 'Cause anyone knows that Reflection is the Spawn-of-Satan-and-Represents-All-That-Is-Truly-Evil-in-the-World (credits to Scott Hanselman for the name :) )
F.D.Castel
+1  A: 

I'd try something like this from the following link:

string path;
path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
MessageBox.Show( path );
Mat Nadrofsky
A: 

A tad shorter:

AppDomain.Current.BaseDirectory
rslite
+2  A: 

Try the implimentation below. The CodeBase property is the most reliable way to get the location, and then the rest of the code converts it into standard Windows format (instead of a URI).

    static public string AssemblyLoadDirectory
    {
        get
        {
            string codeBase = Assembly.GetCallingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            return Path.GetDirectoryName(path);
        }
    }
John Sibly