Click here to Skip to main content
15,887,420 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is what i have tried so far i am converting given path to virtual path but when my website is not in the same folder it will create error so i want a code that converts what ever the physical path is will be converted to virtual path like this

physical path:: C:\Users\NET DEVELOPMENT\Desktop\AgentWeb

and i want it to get converted to virtual path like this
virtual path:: ~/AgentImages/

here agent images is a folder in my agentweb mail folder and the code is like this


C#
protected void btnSubmit_Click(object sender, EventArgs e)
   {
       int id = Convert.ToInt32(Session["AgentMasterID"]);
       string temp = "";
       if (fuProducImage.HasFile)
       {
           if (CheckFileType() == true)
           {

               string directoryPath = Server.MapPath("~/AgentImages/") + id;
               string vitualp = directoryPath.Replace(@"C:\Users\NET DEVELOPMENT\Desktop\AgentWeb", "~").Replace(@"\", "/");
               string filepath = directoryPath + "/" ;
               if (!Directory.Exists(MapPath(filepath)))
               {

                   Directory.CreateDirectory(MapPath(directoryPath + "/"));
                  fuProducImage.SaveAs(MapPath(filepath+fuProducImage.FileName));
                  temp = directoryPath + fuProducImage.FileName;
               }
               else
               {

                   fuProducImage.SaveAs(MapPath(filepath + fuProducImage.FileName));
                   temp = filepath + fuProducImage.FileName;
               }

               //fuProducImage.SaveAs(MapPath("~/AgentImages/" + fuProducImage.FileName));


               lblPimage.Visible = true;
               lblPimage.Text = "Image Uploaded Sucesfully";
           }
       }


pls help me on this
Posted

You first must to understand that not all physical paths can be converted to virtual path, for that the physical path MUST be a child of the current application's root path!
If that is true you can simply 'cut' the root from the physical path and you have a virtual path...
C#
public string ReverseMapPath( string Path )
{
    string szRoot = Server.MapPath ( "~" );

    if ( Path.StartsWith ( szRoot ) )
    {
        return("~/" + Path.Replace ( szRoot, string.Empty ).Replace ( @"\", "/" ));
    }
}
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 7-Jan-15 8:07am    
Yes that's right that not all physical path can be converted to virtual path. It has to be the child of the Application.
Parth Mashroo 8-Jan-15 6:09am    
yes sir that's what i have done i have cut down the path with C drive to whatever the change is.
Now if i move my files to another directory then i have change that in my code too so is there a way to remove that!!?
Parth Mashroo 12-Jan-15 0:45am    
How to use the above code in my code so that i can do some editing!!
Kornfeld Eliyahu Peter 12-Jan-15 2:17am    
Editing? Of what?
Looking at the code you've posted, you've started with a virtual path (~/AgentImages/), and mapped it to the corresponding physical path. Then you try to convert the physical path back to a virtual path, before mapping the virtual path back to a physical path again.

You should be able to replace that with the following:
C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
    int id = Convert.ToInt32(Session["AgentMasterID"]);
    string temp = "";
    if (fuProducImage.HasFile)
    {
        if (CheckFileType() == true)
        {
            string directoryPath = Server.MapPath("~/AgentImages/") + id;
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            temp = Path.Combine(directoryPath, fuProducImage.FileName);
            fuProducImage.SaveAs(temp);

            lblPimage.Visible = true;
            lblPimage.Text = "Image Uploaded Sucesfully";
        }
    }
}
 
Share this answer
 
Comments
Parth Mashroo 8-Jan-15 2:27am    
you see sir i am using that code to create a directory dynamically i.e. if directory does not exist it will create /AgentImages/ID/ thats it will go inside that folder which was created thats why i used that code so will this code work!!
Richard Deeming 8-Jan-15 8:27am    
If the directory doesn't exist, the code I've posted will create it. (Assuming your application has sufficient permissions to create directories.)

Once you've got the physical path of the directory, there's no need to convert it back to a virtual path.
Try below code sample.

C#
public static string MapPathReverse(string fullServerPath)
    {
        return @"~\" + fullServerPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath,String.Empty);
    }
 
Share this answer
 
Comments
Parth Mashroo 7-Jan-15 8:00am    
sir i dont know why i am using this string code vitualp = directoryPath.Replace(@"C:\Users\NET DEVELOPMENT\Desktop\AgentWeb", "~").Replace(@"\", "/");
actually it showed me an error that a virual path was expected at this line
Directory.CreateDirectory(MapPath(directoryPath + "/"));
so if you have another way of doing this then help me with that!!
Praveen Kumar Upadhyay 7-Jan-15 8:01am    
Is my code not working?
Hi,

Here is a very good article on virtual path and physical path. Please look into it once here.

In your case you can use "~" for maping the solution directory and then you can have the customized maping path for immages.

Thanks
Sisir Patro
 
Share this answer
 
Comments
Parth Mashroo 8-Jan-15 2:45am    
Yes sir the first thing i read was that article i understood a lot of things but see sir if i move my website from one location to another i have to change that path so i want to do it dynamically irrelative of any path.
and thank you sir for your reply.
[no name] 8-Jan-15 8:06am    
If you are changing the folder path of any file then you have to give/change the path name where ever you have changed the location. I don't think your can make it dynamically. Physically changing the path and dynamically accessing the path from code is not possible. Either you are in a wrong way or you are trying something which is not feasible. I would like to learn how I can change the path dynamically from you as per your above comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900