Solved

How to get the relative Path from absolute Path.

Posted on 2014-01-02
6
431 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now