473,513 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 78614
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
9123
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
3129
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
6917
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
3688
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
2885
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
6056
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
5051
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
3144
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
2461
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
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.