Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing a virtual directory in C#

Tags:

asp.net

iis-7

I have a webserver setup in iis called WebServer, in location ex. C:\inetpub\WebServer\ I have created a virtual directory call it virtualdir where the physical path is outside of the path of my webserver for example the Virtual directory points at C:\inetput\virtualdir, everything works fine and I can access files going to www.myserver.com/virtualdir/test.txt, however I was wondering if from my default.aspx page I can reference the virtual directory as part of my project like so

File.Exists("~/virtualdir/test.txt)

or do I have to reference the complete path of the virtual directory like so

File.Exists("C:/inetpub/virtualdir/test.txt")

Thank you,

like image 1000
user541597 Avatar asked Dec 21 '25 16:12

user541597


1 Answers

You need to convert a relative, web based path containting the "~" to a path that file.exists can deal with. in order to do that call server map path to convert one to the other. See http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.100).aspx

like image 125
Mike Beeler Avatar answered Dec 24 '25 04:12

Mike Beeler