How get the virtual path based on the physical path?

Discussion in 'ASP .Net' started by marss, Sep 13, 2007.

  1. marss

    marss Guest

    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
    marss, Sep 13, 2007
    #1
    1. Advertising

  2. marss

    marss Guest

    On 13 , 14:59, marss <> wrote:
    > 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
    marss, Sep 13, 2007
    #2
    1. Advertising

  3. 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
    Michael Nemtsev, MVP, Sep 13, 2007
    #3
  4. marss

    marss Guest

    On 13 , 15:23, Michael Nemtsev, MVP <> wrote:
    > 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
    marss, Sep 13, 2007
    #4
  5. Hi, marss.

    Please see if anything in this sample page fits your needs :

    http://asp.net.do/test/apppath.aspx

    If so, I'll post the code for it. It's all very simple.



    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en espaƱol : http://asp.net.do/foros/
    ======================================
    "marss" <> wrote in message news:...
    > On 13 , 14:59, marss <> wrote:
    >> 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
    >
    Juan T. Llibre, Sep 13, 2007
    #5
  6. marss

    marss Guest

    Thank for answer, Juan

    >> On 13 , 17:38, "Juan T. Llibre" <> wrote:
    >>Please see if anything in this sample page fits your needs :
    >> http://asp.net.do/test/apppath.aspx


    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
    marss, Sep 13, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yoshitha

    How to get physical path

    Yoshitha, Jun 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    5,792
    intrader
    Jun 22, 2005
  2. Sakharam
    Replies:
    2
    Views:
    8,477
    Sakharam
    Sep 1, 2005
  3. =?Utf-8?B?SmVmZiBCZWVt?=

    physical path to virtual path under virtual directory

    =?Utf-8?B?SmVmZiBCZWVt?=, Aug 1, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    1,013
    Eliyahu Goldin
    Aug 1, 2007
  4. Scott Tilton

    Convert Physical path to Virtual path?

    Scott Tilton, Jul 11, 2003, in forum: ASP General
    Replies:
    2
    Views:
    598
    Dhananjay Modak
    Jul 11, 2003
  5. Guest

    Virtual Path and physical root

    Guest, Aug 2, 2004, in forum: ASP General
    Replies:
    4
    Views:
    205
    Guest
    Aug 2, 2004
Loading...

Share This Page