Hi Dw

There is a program that I'm doing and what it does is move a chosen file/folder to a folder on drive "C:\test" and I have a list box that only display the files name inside the Test folder and when the user click the file name on a listbox its then copies the selected file to Desktop but I have 2 problem:
1) The folders can be moved in to the test directory but I don't know how to make the folder name(s) that are inside the test file be displayed on a listbox but without the path it has to be only the folder(s) name.

2) The listbox displays every file but not folders that are inside the test folder even if the file is inside another folder inside the test file the file is displayed but here it come a problem: let say inside the test folder I have another file called 123 and inside the 123 folder I have a file maybe named test.txt the test.txt will be shown on the listbox but when I click it to download it the error is produced and the reason for the error is that I coded it like this:

Dim downld As String = "C:\test\" + ListBox1.SelectedItem
IO.File.Copy(downld, My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & System.IO.Path.GetFileName(downld), True)

Now the problem as you can see is that I can only or I only pointed it to look inside the known directory which is "C:\test" but I can't point it to 123 or look inside the directory 123 so that it will find the file named test.txt so my question is that how can I get a full path of a selected item in a listbox so that I can use it to locate the file and copy it.

Thank you.

Ow what I just think I have to enlighten is that the folders inside the test folder are folders that a user has chosen to move then to the test folder so I can't manually write a folder name in code because the user won't use the same name.

Recommended Answers

All 2 Replies

Get all files and folders in a directory

        Dim f As New DirectoryInfo("C:\test\")
        Dim dirs() As DirectoryInfo = f.GetDirectories("*", SearchOption.AllDirectories)
        For Each d As DirectoryInfo In dirs
            ListBox1.Items.Add(d.Name)
            Dim di As New IO.DirectoryInfo(d.FullName)
            Dim diar1 As IO.FileInfo() = di.GetFiles()
            Dim dra As IO.FileInfo
            For Each dra In diar1
                ListBox1.Items.Add(dra)
            Next
        Next
        ListBox1.Sorted = True

now for the copy issue,use the same code to get all directories (d.fullname) and see if it ends with your item like so

If d.FullName.EndsWith("\" & ListBox1.SelectedItem.ToString) Then
                IO.File.Copy(d.FullName, My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & d.Name, True)
                Exit For
            End If

Gd Luck

Thanks and sorry to take too long to reply I just tried now your code and it produced an access denied error it I select a folder from the ListBox items and also it failed to copy the normal file, I'm not sure that failure in copying is done by that I have 2 forms the first form is the form where there is a listbox and all the items are populated at and now once you select the item from the listbox it that ask you if you would like to retrieve files or not if you click yes then it show form2 which is where you supply the credentials you used earlier and it the credentials are correct that it that form that tries to copy the selected files but not after supplying the correct credentials it act as if it did copy the file and produce a message that the files has been copied while there is no file on the Desktop. Any idea why it fail? Also I think the problem can be with this piece of code "& "\" & d.Name, True" because previously on my code instead of that code I used this " & "\" & System.IO.Path.GetFileName(f + Form1.ListBox1.SelectedItem)" and it was copying but only copy the files that are for instance it will only copy files in "C:/test" but it will populate the files in "C:/test/just" but it won't find these items that are located in the "just" folder for retrieval but do find it for population as they do appear on the listbox but not retrievable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.