A couple of helper methods to work with file paths:
/// <summary>
/// Returns filepath's path that is relative to root
/// </summary>
/// <param name="root"></param>
/// <param name="filepath"></param>
/// <returns>If filepath is not under root, 
/// returns filepath</returns>
public static string GetRelativeFileName(string root, 
    string filepath)
{
    if (filepath.StartsWith(root, 
    StringComparison.OrdinalIgnoreCase))
    {
    if (root.EndsWith("\\"))
        return filepath.Substring(root.Length);
    else
        return filepath.Substring(root.Length+1);
    }
    else
    return filepath;
}

/// <summary>
/// Returns the deepest common path within absoluteRoot 
/// for both path1 and path2.
/// </summary>
/// <param name="path1"></param>
/// <param name="path2"></param>
/// <param name="absoluteRoot"></param>
/// <returns></returns>
public static string GetCommonFolderRoot(string path1, 
    string path2, string absoluteRoot)
{
    if (!absoluteRoot.EndsWith("\\"))
    absoluteRoot += "\\";

    string commonFolder = Path.GetDirectoryName(path1) + "\\";
    if (!commonFolder.StartsWith(absoluteRoot, 
        StringComparison.OrdinalIgnoreCase))
    {
    commonFolder = Path.GetDirectoryName(path2) + "\\";
    if (!commonFolder.StartsWith(absoluteRoot, 
            StringComparison.OrdinalIgnoreCase))
    {
        commonFolder = absoluteRoot;
        return commonFolder;
    }
    }

    ///find out the deepest path within the watch folder 
    ///that all changed files belong to.
    ///there are 4 possible scenarios (c:\Folder\ is root folder):
    ///1. common = c:\Folder\Sub\Sub\, 
    /// item = c:\Folder\Sub\Sub\, 
    /// new common = c:\Folder\Sub\Sub\
    ///2. common = c:\Folder\Sub\Sub\, 
    /// item = c:\Folder\Sub\, 
    /// new common = c:\Folder\Sub\
    ///3. common = c:\Folder\Sub\, 
    /// item = c:\Folder\Sub\Sub\, 
    /// new common = c:\Folder\Sub\
    ///4. common = c:\Folder\Sub\, 
    /// item = c:\Another-Folder\, 
    /// new common = c:\Folder\Sub\
    string path = Path.GetDirectoryName(path2) + "\\";
    if (!path.StartsWith(commonFolder))
    {
    //scenario 2 or 4.
    if (commonFolder.StartsWith(path))
    {
        //scenario 2.
        while (commonFolder.Length > absoluteRoot.Length && 
                commonFolder.Length > path.Length)
        {
        commonFolder = GetParentDirectory(commonFolder);
        }
    }
    }
    return commonFolder;
}

(Excuse the doggy formating)
Tags: ,