Find all directories in directory and return only name

后端 未结 5 1019
无人及你
无人及你 2020-12-21 11:37

I want to find all directories in one directory in vb.net.

I found one script:

For Each Dir As String In Directory.GetDirectories(FolderName)
    Com         


        
相关标签:
5条回答
  • 2020-12-21 11:59
      For Each Dir As String In Directory.GetDirectories(FolderName)
            ComboBox3.Items.Add(IO.Path.GetFileName(Dir))
      Next
    
    0 讨论(0)
  • 2020-12-21 12:10

    Use this to get only the directory name:

    dirName = IO.Path.GetDirectoryName(fullPath)
    
    0 讨论(0)
  • 2020-12-21 12:11

    The simplest way is to use System.IO.DirectoryInfo:

        For Each Dir As String In System.IO.Directory.GetDirectories(FolderName)
            Dim dirInfo As New System.IO.DirectoryInfo(Dir)
            ComboBox3.Items.Add(dirInfo.Name)
        Next
    

    (Obviously, you could parse it manually and extract out the text following the final '\', but I believe that the above is more readable)

    0 讨论(0)
  • 2020-12-21 12:12

    Give this a try

        For Each d As String In IO.Directory.GetDirectories(FolderName)
            'IO.Path.GetFileName
            'The characters after the last directory character in path.
            ComboBox3.Items.Add(IO.Path.GetFileName(d))
        Next
    

    This takes advantage of the fact that you have a list of directories and what IO.Path.GetFileName actually does.

    0 讨论(0)
  • 2020-12-21 12:17

    I think the easiest way would be using String.Replace to remove FolderName from the beginning of the directory full name.

    For Each Dir As String In System.IO.Directory.GetDirectories(FolderName)
        ComboBox3.Items.Add(Dir.Replace(FolderName, String.Empty))
    Next
    
    0 讨论(0)
提交回复
热议问题