【问题标题】:Get specific folders path from a full way with Path class ASP.NET MVC 3使用 Path 类 ASP.NET MVC 3 全面获取特定文件夹路径
【发布时间】:2012-11-02 09:36:00
【问题描述】:

我有以下完整路径:

D:\Test1\Test1_1\Test2_1\Test3_1\Test4_1

如果我打电话

Path.GetFileName(abovePath) 将返回Test4_1

get directory from full path

嗯。

例如,我想得到Test2_1\Test3_1

如果可能的话,如何使用Path 类做到这一点?如果没有,如何进行?

我想要一个通用示例,不要只关注我给定的完整路径。

谢谢。

【问题讨论】:

    标签: c# asp.net-mvc-3


    【解决方案1】:

    使用 DirectoryInfo 和 Parent,例如

    var directoryInfo = new DirectoryInfo(abovePath);
    var parentDirectoryName = directoryInfo.Parent.Name;
    var grandParentDirectoryName = directoryInfo.Parent.Parent.Name;
    

    请注意,您可能希望对这些进行空检查,但这是一般的想法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-31
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多