views:

43

answers:

1

I need to get to a certain file in my ASP.NET MVC project. Have two folders in my same Project, let's call them Main and Reference.

From my Main, I am calling a class from my Reference.csproj file. From there, I need to get to my TargetFile, which also lives in the Reference folder.

When I use AppDomain.CurrentDomain.BaseDirectory i get C:\Project\Main, when I want C:\Project\Reference.

So I thought I could use Environment.CurrentDirectory, which would hopefully point me to the Reference folder instead of the Main, but unfortunately it outputs C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE. I have also tried Directory.GetCurrentDirectory(), but I get the same result.

What I need is for Environment.CurrentDirectory to give me C:\Project\Reference. I think it should give me the Reference folder instead of the Main folder because it is being called from inside Reference, but right now it's not even going near my Project folder.

Is there a way I can do that without defining my directory elsewhere? I really want to do this without hardcoding my file path into my project.

+2  A: 

The common way to locate files that are part of an ASP.NET application (or an ASP.NET MVC for that matter) is to use Server.MapPath("~/folder/somefile.txt") which will automatically be converted to an absolute path where ~ is the root of the web site. No matter what assemblies you are referencing in an ASP.NET application or from where they will all end up in the bin folder.

Darin Dimitrov
My app doesn't seem to take Server.MapPath. Is there a using statement I need to add to my class?
Charmander