none
How to navigate forward & back through a filepath? RRS feed

  • Question

  • Hello folks:

    I have two filepaths:

       C:\This\filepath\being\common\to\both\but\they\fork\differently

       C:\This\filepath\being\common\to\both\however\each\goes\its\own\way

    My programming task is to navigate them, left to right, until the node where they diverge (after the "both" directory) is found.

    The Path class is very helpful, seems to be the one for this task but I have only used it for things like Combine() and GetExtension(). My preliminary attempt is along these lines:

                string parent;
                bool notDone = true;
    
                parent = Path.GetDirectoryName(WhereAmI);
    
                while (notDone)
                {
                    parent = Path.GetDirectoryName(parent);
    
                    if (parent == null)
                    {
                        notDone = false;
                    }
                }
    

    I am taking advantage of the GetDirectory() method which allows me to navigate upstream. However, I need to navigate downstream.

    Any tips and suggestions are most welcome.

    TIA

    Saturday, March 31, 2018 9:26 PM

Answers

  • It seems that such useful method was only implemented in C++.

    It is available in any language.

    In C# :

    string sPath1 = @"C:\This\filepath\being\common\to\both\but\they\fork\differently";
    string sPath2 = @"C:\This\filepath\being\common\to\both\however\each\goes\its\own\way";
    StringBuilder sbString = new StringBuilder(260);
    PathCommonPrefix(sPath1, sPath2, sbString);

    with declarations :

    [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PathCommonPrefixW")]
    public static extern int PathCommonPrefix(string pszFile1, string pszFile2, StringBuilder pszPath);


    • Edited by Castorix31 Saturday, March 31, 2018 10:00 PM
    • Marked as answer by Travis Banger Saturday, March 31, 2018 10:05 PM
    Saturday, March 31, 2018 9:59 PM

All replies

  • You can use PathCommonPrefix

    In your case, it will return "C:\This\filepath\being\common\to\both"

    Saturday, March 31, 2018 9:35 PM
  • I suppose I can always break down the paths' constituent parts, place them in a List<string>, and navigate with regular list iteration.

    Thanks again...

    Saturday, March 31, 2018 9:37 PM
  • You can use PathCommonPrefix

    In your case, it will return "C:\This\filepath\being\common\to\both"

    It seems that such useful method was only implemented in C++.

    Thx.

    Saturday, March 31, 2018 9:40 PM
  • It seems that such useful method was only implemented in C++.

    It is available in any language.

    In C# :

    string sPath1 = @"C:\This\filepath\being\common\to\both\but\they\fork\differently";
    string sPath2 = @"C:\This\filepath\being\common\to\both\however\each\goes\its\own\way";
    StringBuilder sbString = new StringBuilder(260);
    PathCommonPrefix(sPath1, sPath2, sbString);

    with declarations :

    [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PathCommonPrefixW")]
    public static extern int PathCommonPrefix(string pszFile1, string pszFile2, StringBuilder pszPath);


    • Edited by Castorix31 Saturday, March 31, 2018 10:00 PM
    • Marked as answer by Travis Banger Saturday, March 31, 2018 10:05 PM
    Saturday, March 31, 2018 9:59 PM