None
None

Reputation: 5670

File path not taking correctly

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

Answers (3)

Damith
Damith

Reputation: 63105

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

Upvotes: 4

dmay
dmay

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

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

Upvotes: 3

Nilesh
Nilesh

Reputation: 2691

Try using

Server.MapPath('~/App_Data/' + FileName)

Upvotes: 2

Related Questions