How get the virtual path based on the physical path?

M

marss

Server.MapPath("~/page.aspx") returns the physical file path based on
the specified virtual path. Is there any reverse method to get the
virtual path based on the physical path (both pathes belong to the
same web application)?
Usually I do in this way:

virtualFilePath =
physicalFilePath.Replace(physicalRootDirectoryPath,"~/")

Perhaps, is there standard inbuilt method?

Mykola
http://marss.co.ua
 
M

marss

Server.MapPath("~/page.aspx") returns the physical file path based on
the specified virtual path. Is there any reverse method to get the
virtual path based on the physical path (both pathes belong to the
same web application)?

Or, maybe, anyone know how to get virtual files pathes in some virtual
directory without casting the directory virtual path to phisical
path.
I mean without this: System.IO.Directory.GetFiles(Server.MapPath("~/
images"))
Mykola
http://marss.co.ua
 
M

Michael Nemtsev, MVP

Hello marss,

try ResolveUrl

PS: look at Request.ServerVariables("APPL_PHYSICAL_PATH") too

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


m> Or, maybe, anyone know how to get virtual files pathes in some
m> virtual
m> directory without casting the directory virtual path to phisical
m> path
 
M

marss

Hello marss,

try ResolveUrl

PS: look at Request.ServerVariables("APPL_PHYSICAL_PATH") too

Thank you Michael.

Probably, I do not clearly stated the question.
I have a virtual folder path: "~/images".
I want to get virtual pathes to the files in this folder, e.g.: "~/
images/1.gif", "~/images/2.gif" and so on.

Now I do it in this way.
string virtualFolder = "~/images";
string physicalFolder = Server.MapPath(virtualFolder);
string[] files = System.IO.Directory.GetFiles(physicalFolder);
for (int i = 0; i < files.Length; i++)
files = files.Replace(physicalFolder,
virtualFolder).Replace("\\", "/");

Is there a simpler solution?

Mykola
http://marss.co.ua
 
M

marss

Thank for answer, Juan

Unfortunately, none of these methods do that I want.

I wrote the real example of my task above. This code works, but it is
not very good, IMHO.
Too many Replace method calls. Also there is converting from the
virtual path to the physical path and conversely, although I have the
virtual folder path at the input and I want virtual files pathes at
the output.

I am interested whether is there a method like this (pseudo code):

string[] files = ...VirtualDirectory.GetFiles("~/images")
where files is an array of the virtual files pathes: "~/images/1.gif",
"~/images/2.gif" ...

or like this (pseudo code):
string file = ...GetVirtualPath("E:\Inetpub\wwwroot\test\images
\1.gif")
where value of the file should be "~/images/1.gif"

I am not sure whether it is possible, I am just interested.

Thanks again.


Regards,
Mykola
http://marss.co.ua
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top