获取特定级别的目录 [英] Get specific level of directory

查看:35
本文介绍了获取特定级别的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过动态路径和用户名获得原始文件夹名称,并且没有Windows默认目录

i would like to get the original folder name by dynamic path and username, and without Windows default directory

例如,

 C:\Users\dynamic user\Desktop\History\2014-11-03\Spreadsheets\excel.xls
 C:\Users\dynamic user\Desktop\History\record.xls

在这种情况下,我希望它为excel.xls&返回历史记录".record.xls.我试图使用GetFilename()方法.但是它只是返回

In this case, i want it to return "History" for excel.xls & record.xls. I tried to use GetFilename() method. But it just returns

 Spreadsheets - excel.xls
 History      - record.xls

有可能实现吗?感谢您的帮助.

Is it possible to achieve? Thanks for your help.

推荐答案

没有内置的方法可以完成您想要的事情.下面的代码应该可以解决问题:

There is no in-built way of doing what you want. The following code should do the trick:

    public static string GetFolderByLevel(this string path, string baseFolderName, int level)
    {
        if (path == null)
            throw new ArgumentNullException("path");

        if (baseFolderName == null)
            throw new ArgumentNullException("baseFolderName");

        var pathWithoutFile = Path.GetDirectoryName(path);

        var folders = pathWithoutFile.ToString().Split(Path.DirectorySeparatorChar);
        int baseFolderLevel = -1;

        for (int i = 0; i < folders.Length; ++i)
        {
            if (string.Compare(folders[i], baseFolderName, true) == 0)
            {
                baseFolderLevel = i;
                break;
            }
        }

        if (baseFolderLevel == -1)
            throw new ArgumentException(string.Format("Folder '{0}' could not be found in specified path: {1}", baseFolderName, path), "baseFolderName");

        int index = baseFolderLevel + level;

        if (-1 < index && index < folders.Length)
        {
            return folders[index];
        }
        else
            throw new ArgumentOutOfRangeException(string.Format("Specified level is out of range."));
    }

现在,您可以像这样使用它:

Now you can use it like:

string path = @"C:\Users\dynamic user\Desktop\History\Peter\record.xls";

path.GetFolderByLevel("Desktop", -2); //returns "Users"
path.GetFolderByLevel("History", 0); //returns "History"
path.GetFolderByLevel("Desktop", 1); //returns "History"

在您的情况下,如果我没记错的话,您正在寻找 path.GetFolderByLevel("Desktop",1);

In your case, if I'm not mistaken, your are looking for path.GetFolderByLevel("Desktop", 1);

UPDATE :我已经修改了先前的解决方案,以便您可以指定具有部分或完整路径的基本文件夹:

public static string GetFolderByLevel(this string path, string baseFolderPath, int level)
{
    if (path == null)
        throw new ArgumentNullException("path");

    if (baseFolderPath == null)
        throw new ArgumentNullException("baseFolderName");

    var pathFolders = path.Split(new char[] {Path.DirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries);
    var basePathFolders = baseFolderPath.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

    int baseFolderIndex = -1;
    int folderCounter = 0;

    for (int i = 0; i < pathFolders.Length; ++i)
    {
        if (string.Compare(pathFolders[i], basePathFolders[folderCounter], true) == 0)
        {
            if (++folderCounter == basePathFolders.Length)
            {
                baseFolderIndex = i;
                break;
            }
        }
        else
        {
            folderCounter = 0;
        }
    }

    if (baseFolderIndex < 0)
       throw new ArgumentException(string.Format("Folder '{0}' could not be found in specified path: {1}", baseFolderPath, path), "baseFolderName");

    int index = baseFolderIndex + level;

    if (-1 < index && index < pathFolders.Length)
    {
        return pathFolders[index];
    }
    else
        throw new ArgumentOutOfRangeException(string.Format("Specified level is out of range."));
}

现在,您可以像这样使用它: path.GetFolderByLevel(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),1);

Now you could use it like: path.GetFolderByLevel(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), 1);

另一个好处是,如果嵌套文件夹的名称相同,则可以指定基本文件夹的唯一局部路径,以确保该方法选择正确的路径.

An additional advantage would be in case of having nested folders with the same name, you can specify the unique partial path of the base folder to make sure the method picks the right one.

这篇关于获取特定级别的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆