Search code examples
vb.netdirectoryenumeration

get directory name of directory containing directory


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

This is file path,

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

Here text is the folder name.

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


Solution

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

    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:

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