PathCommonPrefix (shlwapi)
Last changed: -85.75.12.132

.
Summary
Compares two paths to determine if they share a common prefix. A prefix is one of these types: "C:\\", ".", "..", "..\\".

C# Signature:

[DllImport("shlwapi.dll", CharSet=CharSet.Auto)]
static extern Int32 PathCommonPrefix(
    [In] String pszFile1,
    [In] String pszFile2,
    [Out] StringBuilder pszPath
    );

VB Signature:

Declare Function PathCommonPrefix Lib "shlwapi.dll" Alias "PathCommonPrefixA" (ByVal pszFile1 As String, ByVal pszFile2 As String, ByVal pszPath As String) As Long

User-Defined Types:

None.

Notes:

pszPath may be null, but must have a capacity of at least MAX_PATH - 1 if supplied. The return value is the length of the common prefix.

Tips & Tricks:

Please add some!

Sample Code:

StringBuilder str = new StringBuilder(MAX_PATH);
Int32 nRet = PathCommonPrefix(
    @"C:\win\desktop\temp.txt",
    @"c:\win\tray\sample.txt",
    str
    );
// Result: nRet == 6 && str.ToString() == @"C:\win"

Alternative Managed API:

Do you know one? Please contribute it!

Documentation