【问题标题】:Get Child Directory from Path从路径获取子目录
【发布时间】:2017-03-05 22:46:07
【问题描述】:

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

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

如何获得价值?

【问题讨论】:

  • 引用string.Split
  • 谢谢,我正在寻找一种方法来分割路径。您知道获取 Value CustomCompanyNames 的模式吗?
  • 您能否为问题添加预期值?是Template\bin\file\file.xml吗?如果为真,那么听起来您正在尝试获取相对路径(相对于CustomCompanyNames)。
  • 预期值:CustomCompanyNames,我不需要相对路径 :)

标签: c# path


【解决方案1】:

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

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;
}

【讨论】:

  • 你的第二个区块有错别字。
猜你喜欢
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 2010-10-14
  • 1970-01-01
  • 2015-12-07
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多