Author Topic: Relative Path problem  (Read 4610 times)

0 Members and 1 Guest are viewing this topic.

CADbloke

  • Bull Frog
  • Posts: 342
  • Crash Test Dummy
Re: Relative Path problem
« Reply #15 on: October 28, 2015, 06:55:08 PM »
One quick comment about your code.  Your try/catch block encompasses your argument null exceptions.  Won't they be caught by your catch block which I assume you don't want?  Or since you are throwing them they won't be caught?  I have never tried that before to see what would happen.
Ordinarily, yes but in this case Linker.Crash(Exception e, string message) handles all the problems in those apps. In the command line app it just rants to the log and crashes, in the GUI app it shows a message box, rants to the log and crashes. Actually, now that I think of it, the GUI just crashes too. Uh. Fixing that.

P.S.  If you use resharper then look in to Exceptional as an Addin.  It can force you to either document all exceptions or make sure that you catch them.
I do use that, and yes, it's a good 'un. For those who have never seen it ...

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Relative Path problem
« Reply #16 on: October 29, 2015, 07:48:55 AM »
I was purposely avoiding answering the OP because of the ambiguity of his question.  Now that we're just guessing and throwing code out there, I'll share my two cents.

Code - C#: [Select]
  1. var relativePath = new Uri(fromPath).MakeRelativeUri(new Uri(toPath)); //This has %20 for spaces
  2. var pathName = relativePath.ToString().Replace("%20", " ");
Revit 2019, AMEP 2019 64bit Win 10

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Relative Path problem
« Reply #17 on: October 29, 2015, 11:25:37 AM »
Have no idea why done this way but its all relative(waka waka) anyways.

Code - C#: [Select]
  1.  public static class FileSystemInfoExtensions
  2.     {
  3.  
  4.  
  5.         public static bool HasParent(this DirectoryInfo directInfo, DirectoryInfo parentDirectInfo)
  6.         {
  7.             return FileSystemInfoHasParent(directInfo.Parent, parentDirectInfo);
  8.         }
  9.  
  10.         public static bool HasParent(this FileInfo fInfo, DirectoryInfo parentDirectInfo)
  11.         {
  12.      
  13.             return FileSystemInfoHasParent(fInfo.Directory, parentDirectInfo);
  14.         }
  15.  
  16.         private static bool FileSystemInfoHasParent(DirectoryInfo thisParentDirectInfo, DirectoryInfo parentDirectInfo)
  17.         {
  18.             string parent = parentDirectInfo.FullName.TrimEnd(Path.DirectorySeparatorChar);
  19.             while (thisParentDirectInfo != null)
  20.             {
  21.                 if (thisParentDirectInfo.FullName.Equals(parent, StringComparison.InvariantCultureIgnoreCase))
  22.                 {
  23.                     return true;
  24.                 }
  25.                 thisParentDirectInfo = thisParentDirectInfo.Parent;
  26.             }
  27.             return false;
  28.         }
  29.  
  30.         public static string GetRelativePathTo(this FileSystemInfo fromPath, FileSystemInfo toPath)
  31.         {
  32.             int fromAttr = GetPathAttribute(fromPath);
  33.             int toAttr = GetPathAttribute(toPath);
  34.  
  35.             StringBuilder path = new StringBuilder(260); // MAX_PATH
  36.             if (PathRelativePathTo(
  37.                 path,
  38.                 fromPath.FullName,
  39.                 fromAttr,
  40.                 toPath.FullName,
  41.                 toAttr) == 0)
  42.             {
  43.                 throw new ArgumentException("Paths must have a common prefix");
  44.             }
  45.             return path.ToString();
  46.         }
  47.  
  48.         public static string GetRelativePathTo(this FileSystemInfo fromPath, string toPath)
  49.         {
  50.             int fromAttr = GetPathAttribute(fromPath);
  51.             int toAttr = GetPathAttribute(toPath);
  52.  
  53.             StringBuilder path = new StringBuilder(260); // MAX_PATH
  54.             if (PathRelativePathTo(
  55.                 path,
  56.                 fromPath.FullName,
  57.                 fromAttr,
  58.                 toPath,
  59.                 toAttr) == 0)
  60.             {
  61.                 throw new ArgumentException("Paths must have a common prefix");
  62.             }
  63.             return path.ToString();
  64.         }
  65.  
  66.  
  67.  
  68.  
  69.         private static int GetPathAttribute(FileSystemInfo path)
  70.         {
  71.             if (!path.Exists) throw new FileNotFoundException();
  72.             return String.IsNullOrEmpty(path.Extension) ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL;
  73.  
  74.         }
  75.         private static int GetPathAttribute(string path)
  76.         {
  77.             DirectoryInfo di = new DirectoryInfo(path);
  78.             if (di.Exists)
  79.             {
  80.                 return FILE_ATTRIBUTE_DIRECTORY;
  81.             }
  82.  
  83.             FileInfo fi = new FileInfo(path);
  84.             if (fi.Exists)
  85.             {
  86.                 return FILE_ATTRIBUTE_NORMAL;
  87.             }
  88.  
  89.             throw new FileNotFoundException();
  90.         }
  91.  
  92.  
  93.  
  94.  
  95.         private const int FILE_ATTRIBUTE_DIRECTORY = 0x10;
  96.         private const int FILE_ATTRIBUTE_NORMAL = 0x80;
  97.  
  98.         [DllImport("shlwapi.dll", SetLastError = true)]
  99.         private static extern int PathRelativePathTo(StringBuilder pszPath,
  100.             string pszFrom, int dwAttrFrom, string pszTo, int dwAttrTo);
  101.  
  102.     }
  103.