获取给定根路径的深度目录路径

Get directory path n deph long of a given root path

我正在寻找一种 better/safer/more 优雅的通用方法,它可以从给定路径给我一个深度为 n 的长目录。

我已经创建了一些有用的东西,但它基于字符串解析,所以我希望你能找到更好的解决方案。该方法也可以使用 passed/returned 值的目录信息。

public static string GetDirectoryNDepth(string root, string target, int depth)
    {
        string[] splittedRoot = root.Split('\');
        string[] splittedTarget = target.Split('\');

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < splittedTarget.Length; i++)

            if (i < splittedRoot.Count() + depth)
                sb.Append(String.Format("{0}\", splittedTarget[i]));
            else
                break;

        return sb.ToString();
    }  

示例值:

        //For 3 depth long parametr it should return expected value
        //First case filepath
        string root = @"C\Desktop\temp\MSC\IH";
        string target = @"C:\Desktop\temp\MSC\IH\FirstLevel\SecondLevel\ThirdLevel\dsf - Copy (2).xml";
        string expected = @"C:\Desktop\temp\MSC\IH\FirstLevel\SecondLevel\ThirdLevel";

        //Second case target shorter then depth
        string root = @"C\Desktop\temp\MSC\IH";
        string target = @"C:\Desktop\temp\MSC\IH\FirstLevel\SecondLevel";
        string expected = @"C:\Desktop\temp\MSC\IH\FirstLevel\SecondLevel";

根据您的评论,您可以像这样使用 Path.DirectorySeparatorChar 而不是使用 \ 作为分隔符

public  string GetDirectoryNDepth(string root, string target, int depth)
{
    string[] splittedRoot = root.Split(Path.DirectorySeparatorChar);
    string[] splittedTarget = target.Split(Path.DirectorySeparatorChar);

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < splittedTarget.Length; i++)

         if (i < splittedRoot.Length + depth)
            sb.Append(String.Format("{0}{1}", splittedTarget[i], Path.DirectorySeparatorChar));
         else
            break;

    return sb.ToString(); 
}  

来自 Path.DirectorySeparatorChar 文档:

Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization...

...The value of this field is a slash ("/") on UNIX, and a backslash ("\") on the Windows and Macintosh operating systems.

我发现输入集很特别,root不是以C开头的:是C\

不知道是不是故意的

string root = @"C\Desktop\temp\MSC\IH";
string target = @"C:\Desktop\temp\MSC\IH\FirstLevel\SecondLevel\ThirdLevel\dsf - Copy (2).xml";
string expected = @"C:\Desktop\temp\MSC\IH\FirstLevel\SecondLevel\ThirdLevel";

根据输入样本和代码,这将是高效代码

    public static string GetDirectoryNDepth(string root, string target, int depth)
    {
        var separatorChar = Path.DirectorySeparatorChar;

        int rootSlashCount = root.Split(separatorChar).Length;
        int totalSlashCount = rootSlashCount + depth;


        var iCnt = root.Length;
        while(iCnt < target.Length  && rootSlashCount <= totalSlashCount)
        {
            if (target[iCnt] == separatorChar)
                rootSlashCount++;
            iCnt++;
        }

        var retVal = target.Substring(0, iCnt);
        if (retVal.EndsWith(separatorChar+"") == false)
            retVal += separatorChar;
        return retVal;

    }