1

Inside my asp.net website I am trying to access a file

 FileStream stream = File.Open("/App_Data/" + FileName, FileMode.Open, FileAccess.Read);

I want this code to select an xlsxfile inside my websites app data folder. This code always take the whole path from directory like

'c:\App_Data\w1.xlsx

I don't want this code to go for root URL.I just want to take file inside my website.How can i tweek the code to achieve this

None
  • 5,582
  • 21
  • 85
  • 170
  • Check this out http://stackoverflow.com/questions/11637370/opening-a-file-in-c-sharp-using-filestream – Xavier Jul 29 '13 at 05:30

3 Answers3

4

use Server.MapPath("~/App_Data/" + FileName) instead of "/App_Data/" + FileName

Damith
  • 62,401
  • 13
  • 102
  • 153
3

Try this:

File.Open(Server.MapPath("~") + FileName, FileMode.Open, FileAccess.Read);

Also check out this answer, it has pretty good explanation of MapPath

Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?

Community
  • 1
  • 1
dmay
  • 1,340
  • 8
  • 23
2

Try using

Server.MapPath('~/App_Data/' + FileName)
Nilesh
  • 2,583
  • 5
  • 21
  • 34