Andy Hunt
Andy Hunt

Reputation: 1073

MVC 3 relative path to ./bin/

I'm building an MVC 3 project which requires a reference, by path, in the controller to an assembly sitting in the \bin\ folder of the project.

Can anybody point me in the right direction to constructing a relative path to the \bin\ folder?

Running the project in debug mode in VS 2010 causes the current directory to be C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0, so using .\bin\ looks for a bin folder in that path.

Thanks

Upvotes: 22

Views: 15148

Answers (2)

Rich O'Kelly
Rich O'Kelly

Reputation: 41757

Since you're in a Controller you have access to an instance of HttpServerUtilityBase, which exposes a MapPath method:

var binDirectoryPath = Server.MapPath("~/bin");

This has the added benefit of being trivially unit testable, since the MapPath method is virtual and can be mocked.

Upvotes: 13

Muttok
Muttok

Reputation: 671

You could use current appdomain to get this information.

// 1) get current directory for this loaded assembly
// 2) combine path to get \bin folder 
var path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin");

Upvotes: 47

Related Questions