从Path获取子目录

时间:2016-10-24 08:16:51

标签: c# path

如何拆分路径以获取Value CustomCompanyNames?

C:\Project\v4.0\Tool\Custom\CustomCompanyNames\Template\bin\file\file.xml

我如何获得价值?

1 个答案:

答案 0 :(得分:2)

如果您想获取特定文件的“Template”目录的父目录,可以试试这个:

public string GetTemplateDirectoryParentName(string filePath)
{
    FileInfo fileInfo = new FileInfo(filePath);
    DirectoryInfo directoryInfo = fileInfo.Directory;
    while(directoryInfo.Name != "Tempalte")
    {
        direcotryInfo = direcotryInfo.Parent;
    }
    return direcotryInfo.Parent.Name;
}

您可以通过获取“自定义”目录的子目录来实现另一种方式:

public string GetTemplateDirectoryParentName(string filePath)
{
    FileInfo fileInfo = new FileInfo(filePath);
    DirectoryInfo directoryInfo = fileInfo.Directory;
    while(directoryInfo.Parent.Name != "Custom")
    {
        direcotryInfo = direcotryInfo.Parent;
    }
    return direcotryInfo.Name;
}