I need to access and open a file in web application. I deployed my web application in C:\inetpub\wwwroot\MyWebApplicatiion\. My files are at C:\inetpub\wwwroot\MyFiles\
In my web.config file, I am having setting for my file path:
Server.MapPath is not what you are looking for. It takes a path, relative to your web, and converts it into a file system path. C:\inetpub\wwwroot\Myfiles is not relative to the path of your site since it is outside the web application.
You could try Server.MapPath("~/../" +Properties.Settings.Default.File_Path) since this means relative to the root, then up a directory, but chances are it's not going to work because .Net knows this path is outside the web application. When I'm dealing
with directories that are not within my application, I tend to put a config setting with the actual file system path since it's more independent as the two applications may in essence have absolutely nothing to do with eachother, in which case relative paths
arne't useful since both sites could be moved.
You could also make a virtual directory called myfiles within your application that points to the c:\inetput\wwwroot\myfiles directory. This would let you access it relative to your app root easily.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
Thank you all for your inputs. Adding virtual directory resolved my problem. But, when I try to open the file in MyFiles folder from my web app, it get "ACCESS DENIED" exception. I am on Windows XP OS and I think I gave necessary permissions to the folder
: ASP.NET Machine account, Launch IIS Process Account.
FYI...I am using java script to open the file. Am I missing any other permissoions? Please advice...
hr3
Member
15 Points
32 Posts
How to set relative path in web.config in ASP.NET web application for accessing files?
Nov 16, 2012 08:02 PM|LINK
Hi,
I need to access and open a file in web application. I deployed my web application in C:\inetpub\wwwroot\MyWebApplicatiion\. My files are at C:\inetpub\wwwroot\MyFiles\
In my web.config file, I am having setting for my file path:
<setting name="File_Path" serializeAs="String"> <value>/MyFiles</value> </setting>In my code, I am trying to map the path as: Server.MapPath("~/" +Properties.Settings.Default.File_Path).
It is returning the path as "C:\inetpub\wwwroot\MyWebApplicatiion\MyFiles", but I am expecting C:\inetpub\wwwroot\MyFiles\
To get the expected path, how to set the relative path in web.config file
Thanks in advance...
markfitzme
Star
14345 Points
2218 Posts
Re: How to set relative path in web.config in ASP.NET web application for accessing files?
Nov 16, 2012 09:51 PM|LINK
Server.MapPath is not what you are looking for. It takes a path, relative to your web, and converts it into a file system path. C:\inetpub\wwwroot\Myfiles is not relative to the path of your site since it is outside the web application.
You could try Server.MapPath("~/../" +Properties.Settings.Default.File_Path) since this means relative to the root, then up a directory, but chances are it's not going to work because .Net knows this path is outside the web application. When I'm dealing with directories that are not within my application, I tend to put a config setting with the actual file system path since it's more independent as the two applications may in essence have absolutely nothing to do with eachother, in which case relative paths arne't useful since both sites could be moved.
You could also make a virtual directory called myfiles within your application that points to the c:\inetput\wwwroot\myfiles directory. This would let you access it relative to your app root easily.
oned_gk
All-Star
31055 Points
6356 Posts
Re: How to set relative path in web.config in ASP.NET web application for accessing files?
Nov 17, 2012 01:19 AM|LINK
hr3
Member
15 Points
32 Posts
Re: How to set relative path in web.config in ASP.NET web application for accessing files?
Nov 18, 2012 04:06 PM|LINK
Thank you all for your inputs. Adding virtual directory resolved my problem. But, when I try to open the file in MyFiles folder from my web app, it get "ACCESS DENIED" exception. I am on Windows XP OS and I think I gave necessary permissions to the folder : ASP.NET Machine account, Launch IIS Process Account.
FYI...I am using java script to open the file. Am I missing any other permissoions? Please advice...