Reputation: 7005
I am trying to access a file from an ASP.Net vNext class library using a relative path. The file is (should be) located in the installation folder of the application, and to build the full path I need to get that folder path.
Using System.Reflection.Assembly.GetExecutingAssembly()
, the Location
property is empty. The CodeBase
property contains the following:
CodeBase = "file:///C:/Users/username/.dnx/runtimes/dnx-clr-win-x86.1.0.0-beta7/bin/Microsoft.Dnx.Loader.dll"
How can I get the actual folder where the files being executed are located?
EDIT: The answers here are not valid for ASP.Net 5 as I explained already. - just for the duplicate flagging.
Upvotes: 13
Views: 13141
Reputation: 57823
Try IApplicationEnvironment.ApplicationBasePath
. You can create a constructor in your Startup
class that takes this as an argument, get the value there and then make it available to the class library:
public Startup(IApplicationEnvironment env)
{
_location = env.ApplicationBasePath;
}
Alternatively, if you are getting an instance of the class that needs the location via DI, you can add "Microsoft.Dnx.Runtime.Abstractions" as a dependency in your class library project and add a constructor on the class that takes IApplicationEnvironment
:
public MyClass(IApplicationEnvironment env)
{
Location = env.ApplicationBasePath;
}
In a static class try
ProjectRootResolver.ResolveRootDirectory(Directory.GetCurrentDirectory());
ProjectRootResolver
is in the Microsoft.Dnx.Runtime
namespace.
Upvotes: 7
Reputation: 1214
AppDomain.CurrentDomain.BaseDirectory is probably the most useful for accessing files whose location is relative to the application install directory.
See more at Best way to get application folder path
Hope it helps.
Upvotes: 1