vb.net 获取包含目录的目录名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8173842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:46:57  来源:igfitidea点击:

get directory name of directory containing directory

vb.netdirectoryenumeration

提问by Smith

How to get the folder name from the full path of folder?

如何从文件夹的完整路径中获取文件夹名称?

This is file path,

这是文件路径,

"c:\projects\roott\wsdlproj\devlop\beta2\text"

Here textis the folder name.

这里的文本是文件夹名称。

But i want to get the folder containing text, that is beta2

但我想获取包含文本的文件夹,即beta2

回答by Xardax99

Fri 7/09/2012 10:42 AM io.path.getFileName(filePath) will return the folder name

Fri 7/09/2012 10:42 AM io.path.getFileName(filePath) 将返回文件夹名称

回答by Ahmad Mageed

The Path.GetDirectoryNamemethodcan be used to return "c:\projects\roott\wsdlproj\devlop\beta2", as shown below:

可以使用该Path.GetDirectoryName方法返回"c:\projects\roott\wsdlproj\devlop\beta2",如下图:

Dim filePath As String = "c:\projects\roott\wsdlproj\devlop\beta2\text"
Dim directory As String = Path.GetDirectoryName(filePath)

To get just the name of the parent folder, "beta2", you can split the input and take the second last entry, given that the input is indeed accurate:

要获得父文件夹的名称"beta2",您可以拆分输入并取倒数第二个条目,因为输入确实是准确的:

Dim split As String() = filePath.Split("\")
Dim parentFolder As String = split(split.Length - 2)