Reputation: 5670
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
Upvotes: 1
Views: 106
Reputation: 63105
use Server.MapPath("~/App_Data/" + FileName)
instead of "/App_Data/" + FileName
Upvotes: 4
Reputation: 1325
Try this:
File.Open(Server.MapPath("~") + FileName, FileMode.Open, FileAccess.Read);
Also check out this answer, it has pretty good explanation of MapPath
Upvotes: 3