How to show folder and file name? RRS feed

  • Question

  • Alright, I'm having trouble. See, I have some code that does it job perfectly. It simply populates the listview with all the files of the directory. I've searched the solution numerous times, but I'm not so sure ow to incorporate it into my code. Here it is:

                For Each Folders In My.Computer.FileSystem.GetDirectories(My.Computer.FileSystem.SpecialDirectories.MyPictures & "\")
                    ListView1.Items.Add(Folders, 0)
                For Each I In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyPictures)
                    Dim Desktop_Icon_add As System.Drawing.Icon = Icon.ExtractAssociatedIcon(I)
                    ListView2.Items.Add(I, IconListView.Images.Count - 1)

    At the moment, when it populates the listview, the full file names are each individual's title. The same is the with the folders. How can it be changed to just the name of the file, not the entire directory with the file name on the end. The screenshot below illustrates what it looks like at the moment.

    Thursday, February 13, 2014 3:48 AM


  • Hi,

     You can use the Path.GetFileName() or Path.GetFileNameWithoutExtension() method to get just the filename. However, you may want to set the Tag property of each listviewitem to the full pathname when you add them to the listview. That way if you need to open that file then you can get the selected item from the listview and use the Tag property with the .ToString method to get the full pathname.


    • Edited by IronRazerz Thursday, February 13, 2014 4:13 AM
    • Proposed as answer by Rbie Thursday, February 13, 2014 12:46 PM
    • Marked as answer by Carl CaiModerator Tuesday, February 18, 2014 8:07 AM
    Thursday, February 13, 2014 4:11 AM