0

so far I've been using the System.IO.Path.GetFullPath to compare paths as per the answers in Verifying path equality with .Net.

However, I now encountered an error with links to the desktop, which is something like "https://mycompany-my.sharepoint.com/personal/myname_mycompany_com/Documents/Desktop/". It's a path to an Excel workbook that I retrieve via _workbook.FullName within the code.

Path.GetFullPath throws a NotSupportedException when fed this link.

I have now researched two things.

  1. Trying to avoid the sharepoint path by getting a "local" path to the desktop like "C:\Users\myemployeenumber\OneDrive - mycompany\Desktop"
  2. Trying to find a method to compare paths that has all the advantages of Path.GetFullPath and additionally accepts the sharepoint path.

Unfortunately, after hours of looking I wasn't successful with either of the two. 1) seems to be impossible?! The closest I came with 2) was checking that Path.GetFullPath(Uri(path).LocalPath)) and Uri(path).Host are both equal (ignoring upper/lower case) for the two paths that I am comparing but then the Uri(path).LocalPath does not work for relative paths...

Would be very grateful if someone could help me with either 1) or 2).

Thanks

EDIT, Update on what I ended up doing: We noticed that also other things broke with paths to the Desktop (Sharepoint), e.g. File.Exists(), File.Copy(). While it seems possible to write wrapper functions for all file handling operations that also allow sharepoint paths (e.g. How to download/upload files from/to SharePoint 2013 using CSOM?), we have decided for now to not support sharepoint locations for our application.

Kolti
  • 43
  • 5

1 Answers1

1

"https://mycompany-my.sharepoint.com/personal/myname_mycompany_com/Documents/Desktop/" is an HTTPS URL and you can't reasonably expect that to work with System.IO.Path seeing as that HTTPS webserver isn't even on your system!

All you have to do is compare the relative paths from the HTTPS link to your local OneDrive path to see if they are the same.

If you know the format, you can just write the path comparison yourself. UNTESTED:

using System.Text.RegularExpressions;
    public bool compare(path1, path2)
    {
     if (((path1.StartsWith("C:") && path2.StartsWith("C:"))
         || (path1.StartsWith("http") && path2.StartsWith("http")))
      return path1 == path2
     else if (((path1.StartsWith("C:") && path2.StartsWith("http"))
        || (path2.StartsWith("C:") && path1.StartsWith("http")))
     {
      Regex regex = new Regex(@"https://mycompany-my\.sharepoint\.com/personal/(?<name>myname)_(?<company>mycompany)_com/Documents/(?<path>[/\w]+)");
      string httpvar = path1.StartsWith("http") ? path1 : path2;
      string localvar = path1.StartsWith("C:") ? path1 : path2;
         Match matches = regex.Match(httpvar);
         if (matchResults.Success)
          return ("C:\Users\" + matches["name"].Value + "\OneDrive - " + matches["company"].Value + "\" + matches["path"].Value) == localvar;
       }
     }
    }

Sorry if there's bugs I just wrote it on the spot in this little web window

8vtwo
  • 60
  • 7
  • I'm not trying to compare a local one drive path to its https representation. That's actually impossible since the https one uses the employee name and the local one uses the employee number (sorry, didn't make that clear) and I cannot translate between the two. I just want my utility method ComparePaths(string path1, string path2) to not throw an error if one of the two paths is an https path. But your answer is still helpful in that it made me realise that, even if I want to stay generic, I may have to check first a path starts with https and in that case apply different treatment. – Kolti Apr 02 '21 at 11:44