1

I have a asp.net website that has the following directory:

C:\Users\Desktop\Testing\src\website

I have another folder called "files" that is here:

C:\Users\Desktop\Testing\src\files

from inside my project i am trying to read files from the "files" folder, i am doing it like this:

var path = HttpContext.Current.Server.MapPath("/files"); 

I also tried :

var path = HttpContext.Current.Server.MapPath(".."); 

But it says Failed to map the path '/files'.

What could be the reason for this? could it do something with my IIS? How can i get this working??

Thank you!

Ovi
  • 2,459
  • 9
  • 50
  • 72

2 Answers2

5

You can't do this. The Server.MapPath method only works with folders relative to the root of the web application which in your case is C:\Users\Desktop\Testing\src\website. You cannot go one level up in the hierarchy using this method as you are leaving the domain of control of this ASP.NET application. To achieve this you will have to use an absolute path. For example if you want to read some file which is situated outside of your application:

var data = File.ReadAllText(@"C:\Users\Desktop\Testing\src\files\somefile.txt");
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thank you! how do i use absolute path? – Ovi Sep 18 '11 at 14:14
  • @Ovi, I showed you in my answer. You write the full path to the file. – Darin Dimitrov Sep 18 '11 at 14:15
  • Thanks, why does it say "Access to the path 'C:\Users\\Desktop\Testing\src\files' is denied." i just need to get the path, and then later to get a file from there... – Ovi Sep 18 '11 at 14:22
  • @Ovi, it says this because the account under which your site runs in IIS doesn't have permissions to read this directory. You will have to grant it access. That's one of the reasons why you should avoid accessing files outside of your ASP.NET application. It is recommended to use the special `App_Data` for this purpose. – Darin Dimitrov Sep 18 '11 at 14:23
  • Can you please tell me exactly how to do this? – Ovi Sep 18 '11 at 14:50
  • @Ovi, you add the `App_Data` folder to your ASP.NET application and then store your files inside it. Then you simply use `var myFile = Path.Combine(Server.MapPath("~/App_Data"), "someFile.txt");` to get the absolute path to this file and access it in your application. – Darin Dimitrov Sep 18 '11 at 14:51
  • no, i have to keep it out of the asp.net application, how do i to grant it access from IIS? – Ovi Sep 18 '11 at 14:53
  • @Ovi, you navigate to the folder, right click on it and in the Security tab you grant read (and write permissions if you need to write to) to the account that you use to run your application under IIS. – Darin Dimitrov Sep 18 '11 at 14:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3570/discussion-between-ovi-and-darin-dimitrov) – Ovi Sep 18 '11 at 14:58
0

assuming the websites's virtual directory is mapped to .../src/website you need to get "files" folder like this:

var filesDir = Path.Combine(HttpContext.Current.Server.MapPath("~"), "../files/");
ivan
  • 628
  • 5
  • 14