Get the directory where the execution code is located

I know that in the same directory where my code runs, there are some files. I need to find them and move on to another method:

MyLib.dll
Target1.dll
Target2.dll

Foo(new[] { "..\\..\\Target1.dll", "..\\..\\Target2.dll" });

Therefore, I call System.IO.Directory.GetFiles(path, "*.dll"). But now I need to find out the path:

string path = new FileInfo((Assembly.GetExecutingAssembly().Location)).Directory.FullName)

but is there a shorter way?

+5
source share
2 answers

You can try the property Environment.CurrentDirectory. Please note that depending on the type of application (Console, WinForms, ASP.NET, Windows Service, ...) and the way it is launched, this may behave differently.

+6
source

Environment.CurrentDirectory , , . Directory.SetCurrentDirectory, , , , , .

. ( ) :

private DirectoryInfo ExecutingFolder
{
    get
    {
        return new DirectoryInfo (
            System.IO.Path.GetDirectoryName (
                System.Reflection.Assembly.GetExecutingAssembly().Location));
    }
}
+2

All Articles