By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 2,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

absolute path to relative path conversion

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.