473,288 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

absolute path to relative path conversion

is it possible to get the relative path based on a absolute path in c#?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
7 78596
Hi Rizaan,

I am not 100% sure what you mean by this question.
A relative path is relative to some other path.
Are you asking whether, given two absolute paths, you can automatically work
out the relative path between them?

Rob

"Rizaan Jappie" <ri*****@korbitec.com> wrote in message
news:eD******************@TK2MSFTNGP10.phx.gbl...
is it possible to get the relative path based on a absolute path in c#?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Sorry for being so vague Rob

what i need is the following :

If i have an absolute path to a file e.g.
c:\rizaan\pathsquestion\absolutepath\class1.cs

how would i go about getting the relative path in c# for class1.cs using
'c:\rizaan\pathsquestion\absolutepath\' as the source directory?

does this make sense at all?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
If I understand what you want to do, when you want to do this in C# code,
setting Environment.CurrentDirectory and using only "relative paths" after
that would do what you expect

string path =
System.IO.Path.GetDirectoryName(@"c:\rizaan\pathsq uestion\absolutepath\class
1.cs");
Environment.CurrentDirectory = path;

and you can use relative paths from here, unless CurrentDirectory is changed
"Rizaan Jappie" <ri*****@korbitec.com> wrote in message
news:em****************@TK2MSFTNGP10.phx.gbl...
Sorry for being so vague Rob

what i need is the following :

If i have an absolute path to a file e.g.
c:\rizaan\pathsquestion\absolutepath\class1.cs

how would i go about getting the relative path in c# for class1.cs using
'c:\rizaan\pathsquestion\absolutepath\' as the source directory?

does this make sense at all?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4
Hi Rizaan,

If I understand correctly, what you are trying to do is easy in the example
you give as you are effectively just after the filename:

FileInfo fi = new FileInfo ( abPath ); // get a FileInfo for the file
specified in the absolute path
string relPath = fi.Name; // the name property returns the unqualified name
of the file

However, if the file you are after is in another directory then this is
quite a lot harder. I can't think of a way off the top of my head without
doing some sort of 'directory walk'.
You could walk from you absolute path to the root of your drive and then
walk down the path to your file, building the relative path as you go along.
This would work, but may not give you the most concise relative path
possible.

I am still not 100% sure I have understood what you are trying to do though.
Is it possible that the file you are after is in a different directory to
the absolute path given? Are you trying to build a relative path between a
directory and a file in another?

Apologies if it is obvious what you are saying and I am just missing the
point !

Rob

"Rizaan Jappie" <ri*****@korbitec.com> wrote in message
news:em****************@TK2MSFTNGP10.phx.gbl...
Sorry for being so vague Rob

what i need is the following :

If i have an absolute path to a file e.g.
c:\rizaan\pathsquestion\absolutepath\class1.cs

how would i go about getting the relative path in c# for class1.cs using
'c:\rizaan\pathsquestion\absolutepath\' as the source directory?

does this make sense at all?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #5
what im trying to do is a bit hard to explain but here goes. I have been
assigned to write a program that creates VS.NET 2003 project files
(.csproj files) to a specified folder. If you look at the spec of the
.csproj file you will notice under the '<Files><Include><File>' section
that each file present in a particular project e.g. classes, forms etc
are all stored using a relative path e.g.

<Files>
<Include>
<File
RelPath = "class1.cs"
Link = "..\..\..\..\..\CSharp\class1.cs"
SubType = "Code"
BuildAction = "Compile"
/>
</Include>
</Files>

i have the absolute path of class1.cs but would like to get the relative
path of class1.cs so as to build the .csproj correctly...also i have
created an instance of FileInfo so i do have the properties of class1.cs
on hand...also note that the class1.cs file is linked to the project and
not always present in the same directory of the project.

thanks for the previous replies..

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
Hi Rizaan,

Well - your problem makes sense but I can't think of a simple solution to be
honest - sorry !
It seems to me that the best approach is something along these lines:

Take the absolute paths of your project file and your class file
e.g.
string projPath = "c:\dev\projects\myProj\myProj.csproj"
string classPath = "c:\dev\projects\myProj\src\blah\class1.cs"

Now examine these two paths and chop off the common part at the start of the
path

projPath = "myProj.csproj"
classPath = "src\blah\class1.cs"

Now parse the longer of the paths, generating a relative path. e.g. every
time you hit a \ symbol, add a .. to your rel path etc.
This should work but there are a number of caveats that spring to mind:
- will the two paths always be in the same format? ie file://c:\dev.... vs
c:\dev....
- will the root be quoted in the same way? ie c:\dev... vs
\\mymachine\c$\dev....
- will the slashes be the same way round ?
- etc

Hope this helps a bit, sorry for not having a more elegant solution.

Regards,

Rob
http://roblevine.blogspot.com

"Rizaan Jappie" <ri*****@korbitec.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
what im trying to do is a bit hard to explain but here goes. I have been
assigned to write a program that creates VS.NET 2003 project files
(.csproj files) to a specified folder. If you look at the spec of the
csproj file you will notice under the '<Files><Include><File>' section
that each file present in a particular project e.g. classes, forms etc
are all stored using a relative path e.g.

<Files>
<Include>
<File
RelPath = "class1.cs"
Link = "..\..\..\..\..\CSharp\class1.cs"
SubType = "Code"
BuildAction = "Compile"
/>
</Include>
</Files>

i have the absolute path of class1.cs but would like to get the relative
path of class1.cs so as to build the .csproj correctly...also i have
created an instance of FileInfo so i do have the properties of class1.cs
on hand...also note that the class1.cs file is linked to the project and
not always present in the same directory of the project.

thanks for the previous replies..

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #7
Hi,

Some code for You:

private static string EvaluateRelativePath(string mainDirPath
, string absoluteFilePath) {
string[]
firstPathParts=mainDirPath.Trim(Path.DirectorySepa ratorChar).Split(Path.DirectorySeparatorChar);
string[]
secondPathParts=absoluteFilePath.Trim(Path.Directo rySeparatorChar).Split(Path.DirectorySeparatorChar );

int sameCounter=0;
for(int i=0; i<Math.Min(firstPathParts.Length,
secondPathParts.Length); i++) {
if(
!firstPathParts[i].ToLower().Equals(secondPathParts[i].ToLower()) ) {
break;
}
sameCounter++;
}

if( sameCounter==0 ) {
return absoluteFilePath;
}

string newPath=String.Empty;
for(int i=sameCounter; i<firstPathParts.Length; i++) {
if( i>sameCounter ) {
newPath+=Path.DirectorySeparatorChar;
}
newPath+="..";
}
if( newPath.Length==0 ) {
newPath=".";
}
for(int i=sameCounter; i<secondPathParts.Length; i++) {
newPath+=Path.DirectorySeparatorChar;
newPath+=secondPathParts[i];
}
return newPath;
}

HTH
Marcin
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joe Cybernet | last post by:
Is there any function for combining an absolute and a relative URL to result in an absolute URL? Like if I have http://www.domain.com and "../images/1.jpeg" it will evaluate to...
3
by: Peter Taurins | last post by:
Hi there. I have an included file (header.php) that contanis a reference to a graphic. If I stay at the root level, then I can control the relative path of the image. eg. images/imagename.jpg ...
3
by: rajuvk | last post by:
I am setting up a website with a number of folders like: / (the document root) /user /admin/ /content in the /user folder there is a flie "userlogged.php", which I want to include in every...
9
by: Stuart | last post by:
Hi All, I got a challenge to make the same APS/Script/Html run on different web roots. I can not use relative pathing in a lot of cases. We use lots of included files so depending on where that...
15
by: Nick K. | last post by:
I recently began maintenance work on a production web server that is located in the root directory of a web server. I moved this into a sub web on my local web server in order to do work on it. I...
4
by: Vitali Gontsharuk | last post by:
Hallo! When using the XPATH document() function to load a new XML document, we are coming across problems, because XALAN seems to have problems with absolute paths. XALAN always assumes that the...
19
by: Jerry M. Gartner | last post by:
Greetings: What is the best way to resolve paths within a document regardless of what path it is opened under? For example: I have x.php and it contains <img src="images...">, (amongst other...
6
by: Jon Slaughter | last post by:
do I have to prefix every absolute path with document root to get it to work? For some reason I thought that prefixing a path with '/' or './' with make it absolute w.r.t to document root but I...
13
by: Nathan Sokalski | last post by:
I have a page in my site that I need the absolute url of. Is there a function in .NET to which you can pass a relative url or something such as "~/mydirectory/mypage.aspx" which will return an...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.