absolute path to relative path conversion

  • Thread starter Thread starter Rizaan Jappie
  • Start date Start date
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
 
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?
 
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\pathsquestion\absolutepath\class
1.cs");
Environment.CurrentDirectory = path;

and you can use relative paths from here, unless CurrentDirectory is changed
 
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
 
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..
 
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
 
Hi,

Some code for You:

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

int sameCounter=0;
for(int i=0; i<Math.Min(firstPathParts.Length,
secondPathParts.Length); i++) {
if(
!firstPathParts.ToLower().Equals(secondPathParts.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;
}
return newPath;
}

HTH
Marcin
 
Back
Top