Last post Nov 18, 2012 04:06 PM by hr3
Nov 16, 2012 08:02 PM|hr3|LINK
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">
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...
Nov 16, 2012 09:51 PM|markfitzme|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.
Nov 17, 2012 01:19 AM|oned_gk|LINK
Nov 18, 2012 04:06 PM|hr3|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...