Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取Path.GetDirectory或Server.MapPath中父文件夹旁边的文件夹_C#_Asp.net - Fatal编程技术网

C# 获取Path.GetDirectory或Server.MapPath中父文件夹旁边的文件夹

C# 获取Path.GetDirectory或Server.MapPath中父文件夹旁边的文件夹,c#,asp.net,C#,Asp.net,考虑两个路径,一个在解决方案中,另一个在IIS中: 解决方案路径: C:\Users\userid\Desktop\File\Solution\RootName\Folder1\Folder1a\the.aspx IIS路径 C:\inetpub\wwwroot\RootName\webApplication\Folder1\Folder1a\the.aspx 有没有一种方法可以使用目录或服务器或任何东西来获取父目录后的第一个文件夹名 在上面给定的ie中,Folder1是父文件夹之后的第

考虑两个路径,一个在解决方案中,另一个在IIS中:

解决方案路径:

  • C:\Users\userid\Desktop\File\Solution\RootName\Folder1\Folder1a\the.aspx
IIS路径

  • C:\inetpub\wwwroot\RootName\webApplication\Folder1\Folder1a\the.aspx
有没有一种方法可以使用
目录
服务器
或任何东西来获取父目录后的第一个文件夹名

在上面给定的ie中,Folder1是父文件夹之后的第一个文件夹

我可以从文件路径(在本例中为Folder1a)中获取相反的文件夹名,我可以通过迭代得到答案:

Path.GetFileName(Path.GetDirectoryName(file))

但问题是,文件夹可以任意嵌套:(folder1a\folder1a1\folder1a1a…)

如何获得给定文件路径的父目录后的第一个文件夹名

作为旁白,
如果没有foldername(例如Folder1不存在),答案应输出“”(空)

使用
转到上一个目录,例如:

C:\test\..\
相当于:

C:\

使用
Directory.GetDirectories
查找子目录。它有一个重载,您也可以在其中传递搜索模式:

string[] GetDirectories(string path, string searchPattern)
搜索模式 类型:System.String

要与路径中的子目录名称匹配的搜索字符串。 此参数可以包含有效的文本和 通配符(请参见备注),但不支持正则 表情


使用
转到上一个目录,例如:

C:\test\..\
相当于:

C:\

使用
Directory.GetDirectories
查找子目录。它有一个重载,您也可以在其中传递搜索模式:

string[] GetDirectories(string path, string searchPattern)
搜索模式 类型:System.String

要与路径中的子目录名称匹配的搜索字符串。 此参数可以包含有效的文本和 通配符(请参见备注),但不支持正则 表情

试试这个:

string filename = string.Format("{0}{1}", System.AppDomain.CurrentDomain.BaseDirectory, @"Folder1\Folder1a\the.aspx");
试试这个:

string filename = string.Format("{0}{1}", System.AppDomain.CurrentDomain.BaseDirectory, @"Folder1\Folder1a\the.aspx");

下面是我尝试过的有效方法

因为我只需要输出单词后面的第一页,所以我迭代了
Path.GetDirectoryName
Path.GetFileName
,直到可以匹配根文件夹

例:


我仍然相信会有更好的答案,尽管下面是我尝试过的有效答案

因为我只需要输出单词后面的第一页,所以我迭代了
Path.GetDirectoryName
Path.GetFileName
,直到可以匹配根文件夹

例:


我仍然相信会有更好的答案,不过

谢谢。。但是我如何到达下一个目录呢?使用
directory.GetDirectories
并匹配您的文件夹名。谢谢。。但是如何到达下一个目录呢?使用
directory.GetDirectories
并匹配您的文件夹名。