Many people are not unfamiliar with them, in a number of Web applications, using Server.MapPath and Request.physicalapplicationpath to manipulate the directory/file is a mixed chance, I used to mix, but the time is long. Found Request.physicalapplicationpath a little "trouble"?
In fact, in the general application, do not feel the difference between the two, and in some of the existence of "virtual directory", the existence of multi-level subdirectories, the need for virtual directory file operation of the site, you can know their differences. My summary is as follows:
1. File operations relative to the current path:
You can use Server.MapPath ("Test.txt"), Server.MapPath ("A/test.txt"), Server.MapPath (".. /b/test.txt "), this time using Request.physicalapplicationpath is difficult to locate the file;
2, the absolute path of the file operation:
At this time both can be, a little note is that Server.MapPath is absolutely "oriented" to the site, not because the "virtual directory" itself has different properties, Request.physicalapplicationpath is not the case.
For example: The primary site points to d:/wwwroot/, and the virtual directory points to e:/wwwroot/a/, named a.
So if I want to operate the virtual directory under the E:/wwwroot/a/test.txt, you can use Server.MapPath ("/a/test.txt"), or directly use Server.MapPath ("~/test.txt");
So how do you write with Request.physicalapplicationpath?
Many people are: Request.physicalapplicationpath + "test.txt", I said not necessarily, why?
Because if the virtual directory has a separate application pool, Request.physicalapplicationpath is e:/wwwroot/a/, after the addition of Test.txt is e:/wwwroot/a/test.txt;
But if the virtual directory does not have a separate application pool, Request.physicalapplicationpath is d:/wwwroot/, no matter how you add it, you can't locate the file I want to find. Oh......
3. File operations across virtual directories:
For example I am now operating virtual directory B under the Test2.txt file, using Server.MapPath ("/b/test2.txt") there is no problem (if I have permission to operate, no permissions is another matter).
And Request.physicalapplicationpath can not find other virtual directories under the file, do not believe who try, then tell me how?!!
To sum up, I personally tend to use Server.MapPath later.
Talking about Server.MapPath and Request.physicalapplicationpath