Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2004

Tip: Looking for answers? Try searching our database.

RelativePath

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick Strahl [MVP] - 29 Oct 2004 22:32 GMT
Does anybody know if there's some sort of function available to create a
relative path from a full path? Basically the reverse of Path.GetFullPath()?

Basically I need to pass in a path and a base path (usually the current
directory) and try to get back something like:

.\somefile.txt
..\..\somefile.txt

Before I write my own, i figure I better check <g>..

+++ Rick ---

Signature

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web

Mattias Sj?gren - 30 Oct 2004 01:05 GMT
Rick,

>Does anybody know if there's some sort of function available to create a
>relative path from a full path? Basically the reverse of Path.GetFullPath()?

Not in the framework, but there is the PathRelativePathTo Win32 API

http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/shlwapi
/path/pathrelativepathto.asp


Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Rick Strahl [MVP] - 30 Oct 2004 07:45 GMT
Thanks Mattias,

I ended up writing my own routine, which won't require interop. It's not
tested through all the way, but it works for what I need at this point:

/// <summary>
/// Returns a relative path string from a full path.
/// </summary>
/// <param name="FullPath">The path to convert. Can be either a file or a
directory</param>
/// <param name="BasePath">The base path to truncate to and replace</param>
/// <returns>
/// Lower case string of the relative path. If path is a directory it's
returned without a backslash at the end.
///
/// Examples of returned values:
///  .\test.txt, ..\test.txt, ..\..\..\test.txt, ., ..
/// </returns>
public static string GetRelativePath(string FullPath, string BasePath )
{
  // *** Start by normalizing paths
  FullPath = FullPath.ToLower();
  BasePath = BasePath.ToLower();

  if ( BasePath.EndsWith("\\") )
     BasePath = BasePath.Substring(0,BasePath.Length-1);
  if ( FullPath.EndsWith("\\") )
     FullPath = FullPath.Substring(0,FullPath.Length-1);

  // *** First check for full path
  if ( FullPath.IndexOf(BasePath) > -1)
     return  FullPath.Replace(BasePath,".");

  // *** Now parse backwards
  string BackDirs = "";
  string PartialPath = BasePath;
  int Index = PartialPath.LastIndexOf("\\");
  while (Index > 0)
  {
     // *** Strip path step string to last backslash
     PartialPath = PartialPath.Substring(0,Index );

     // *** Add another step backwards to our pass replacement
     BackDirs = BackDirs + "..\\" ;

     // *** Check for a matching path
     if ( FullPath.IndexOf(PartialPath) > -1 )
     {
        if ( FullPath == PartialPath )
           // *** We're dealing with a full Directory match and need to
replace it all
           return
FullPath.Replace(PartialPath,BackDirs.Substring(0,BackDirs.Length-1) );
        else
           // *** We're dealing with a file or a start path
           return FullPath.Replace(PartialPath+ (FullPath == PartialPath ?
"" : "\\"),BackDirs);
     }
     Index = PartialPath.LastIndexOf("\\",PartialPath.Length-1);
  }

  return FullPath;
}

Signature

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web

> Rick,
>
> >Does anybody know if there's some sort of function available to create a
> >relative path from a full path? Basically the reverse of Path.GetFullPath()?
>
> Not in the framework, but there is the PathRelativePathTo Win32 API

http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/shlwapi
/path/pathrelativepathto.asp


> Mattias

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.