0

I'm trying to find a root folder on the server. Nothing works, or works wrong

@{ string [] list = Directory.GetFiles(Directory.GetCurrentDirectory()); foreach(var item in list) { <p>@item</p> } } 

Shows exactly what I don't need

@{ string [] list = Directory.GetFiles("~/"); foreach(var item in list) { <p>@item</p> } 

}

Doesn't work

How to solve this?

2

3 Answers 3

2

You can use the following:

Server.MapPath("~"); 

The ~ is always the root in an ASP.NET application. If you insert it in a Razor view it will be translated to the appropriate path for "external access", e.g. ~/Content/site.css will be converted to /Content/site.css if the site is hosted in the root directory or to e.g. /Page1/Content/site.css if it is hosted at a virtual directory called Page1. Therefor to get the absolute path you need to "map it".

If you do not have access to Server you can also use HostingEnvironment.ApplicationPhysicalPath.

Sign up to request clarification or add additional context in comments.

Comments

1

Try

Server.MapPath("~"); 

or:

HostingEnvironment.ApplicationPhysicalPath 

Comments

0

Refer http://msdn.microsoft.com/en-us/library/ms178116.aspx. For your ease i have modified your code:

string [] list = Directory.GetFiles(Server.MapPath("~")); StringBuilder sb=new StringBuilder(); foreach (var item in list) { sb.Append(item + "\n"); } Response.Write(sb); 

Even though I am giving you answer, I will suggest try to have path which is configurable in web.config or in the database because fixing paths are like hard coding the login ID :). Hope you got me.

Thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.