Since you are using MapPath, you are returning the physical path (\) from the virtual path (/).
Creating a DirectoryInfo object or using the Path utility method starting with your child application root will not necessarily give you what you expect if your virtual parent and virtual grandparents do not have the same hierarchy as your physical directory structure.
My applications are not physically nested according to the depth of Url. This can also occur if a virtual directory is involved.
Assuming the grandparent application is two virtual folders, you get the physical path ...
string physicalGrandparentPath = HttpContext.Current.Server.MapPath("~/../../");
Using this, you will be protected from any virtual games that will be moved in IIS settings.
I used this to understand how far I can go. I did not get an HttpException until I tried to go above wwwroot.
source share