Edward
Edward

Reputation: 7424

How to select a folder relative to root in asp.net website?

I am trying to select all the files from a folder in my website and store them in a collection. The problem is that when I run the website it is not selecting the folder in my website:

This is the basic structure: [Root Folder] --> [FilesFolder]

Here is the code I am using:

DirectoryInfo dir = new DirectoryInfo("FilesFolder");

But it is showing this at runtime as the location of the folder:

C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\FileUploads

Is there a way to select the folder relative to the root of the website?

I am using C# with ASP.NET 3.5

Upvotes: 1

Views: 3405

Answers (2)

Brian Mains
Brian Mains

Reputation: 50728

You need to provide the full path to the web site when accessing it through the directory as in:

new DirectoryInfo("c:\inetpub\wwwroot\RootFolder\FilesFolder")

If you are trying to do this within an ASP.NET web site code, you can use Server.MapPath as in:

string path = Server.MapPath("~/FilesFolder");

Upvotes: 7

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102408

Use:

Server.MapPath("~/FilesFolder");

More about it here: http://msdn.microsoft.com/en-us/library/ms178116.aspx

Upvotes: 1

Related Questions