How to get virtual directory physical path

As we know that a virtual direcoty can be linked to a folder with a diffrent name, how can I get the physical path of a virtual directory ?

I’ve been trying with HttpContext.Current.server.MapPath but it returns me the physic path plus the path I send in parameter even if the directory doesn’t even exist or if it exists with a diffrent name.

Exemple :

C:blablaSitesApplication1Imaageesss
– On disc

Application1Images (In ISS, my virutal directory)

But if I do a MapPath on “/Images” it will never give me
C:blablaSitesApplication1Imaageesss but
C:inetpubwwwrootImages which is not the real directory linked to.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Server.MapPath("~/Images")

is the correct way to go about it as “~” references the root of your application.

Method 2

This is what worked for me:

string physicalPath =    
System.Web.Hosting.HostingEnvironment.MapPath(HttpContext.Current.Request.ApplicationPath);

Method 3

What if you try this little snippet?

string physicalPath = HttpContext.Current.Request.MapPath(appPath);

Method 4

After some more research I was able to create a method to get the physical path of a virtual IIS directory:

public static string VirtualToPhysicalPath(string vPath) {
    // Remove query string:
    vPath = Regex.Replace(vPath, @"?.+", "").ToLower();

    // Check if file is in standard folder:
    var pPath = System.Web.Hosting.HostingEnvironment.MapPath("~" + vPath);
    if (System.IO.File.Exists(pPath)) return pPath;

    // Else check for IIS virtual directory:
    var siteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
    var sm = new Microsoft.Web.Administration.ServerManager();
    var vDirs = sm.Sites[siteName].Applications[0].VirtualDirectories;
    foreach (var vd in vDirs) {
        if (vd.Path != "/" && vPath.Contains(vd.Path.ToLower())) pPath = vPath.Replace(vd.Path.ToLower(), vd.PhysicalPath).Replace("/", "\");
    }
    return pPath;
}

Caveat: this solution assumes that you only have a root application (Applications[0]).

Method 5

The following should work just fine:

var physicalPath = HostingEnvironment.MapPath("~/MyVirtualDirectory");

Method 6

This might answer your question:

http://msdn.microsoft.com/en-us/library/system.web.httprequest.physicalpath.aspx

However, I can’t currently provide an example, because I have got a lot of work to do. When I’ll find some time I’ll send detailed information.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x