Solved

How to get the relative Path from absolute Path.

Posted on 2014-01-02
6
442 Views
Last Modified: 2014-01-06
How to get the relative Path from absolute Path. I am using this in .Net 3.5 application.

I want  \Details\Page1.aspx  from
c:\Projects\NewApplication\NewApplication\Details\Page1.aspx  


Thanks
0
Comment
Question by:KavyaVS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 50 total points
ID: 39752305
Here's sample code snippet:
var physicalpath= "c:\Projects\NewApplication\NewApplication\Details\Page1.aspx";
Response.Write(physicalpath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"],"/"));

Open in new window

You might also have to replace \ by /
0
 
LVL 7

Assisted Solution

by:Kishan Zunjare
Kishan Zunjare earned 50 total points
ID: 39753314
Hi,

Convert absolute path back into a relative web path :

This migh work;
String RelativePath = AbsolutePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);

Open in new window

Thanks
Kishan
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 39753910
For your requirement, I would like to know what you are trying to do exactly?

It will help us to answer your question as per that.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:KavyaVS
ID: 39757887
I am doing the directory browsing in the .Net application.
If the page URL is c:\Projects\NewApplication\NewApplication\Details\Page1.aspx,
I am trying to get  \Details\Page1.aspx from Page URL.

Thanks
0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 400 total points
ID: 39758540
Hi,

Here is the solution -

static void Main(string[] args)
        {
            const string filePath = @"c:\Projects\NewApplication\NewApplication\Details\Page1.aspx";

            var lastDirectory = Path.GetFileName(Path.GetDirectoryName(filePath));
            var fileName = Path.GetFileName(filePath);

            Console.WriteLine("The path is: " + filePath);

            Console.WriteLine("\nThe file name with last directory is: " + lastDirectory + @"\" + fileName);

            Console.ReadKey();
        }

Open in new window

0
 

Author Closing Comment

by:KavyaVS
ID: 39760713
Thanks
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question