For a start, you are using the wrong control if you want to get a list of all files in a directory based on a folder path. (Unless you want specifically select these files only).
You are using the OpenFileDialog when you would be better to use the OpenFolderDialog. I would only be using the OpenFileDialog if you want to choose which files you want to add manually, otherwise you could use recursive measures to do the work for you.
The open folder dialog is all you need to return the folder path that your user has selected.
You can use
System.IO.DirectoryInfo to return directory info, and
System.IO.FileInfo to return file info. To return a list of the files in that directory you can use the System.IO.Path as you have in your above example.
You don't need this because the path will never be null if it can be selected. :)
If Not String.IsNullOrEmpty(txtFolderPath.Text) Then
I rewrote it using the folderdialog instead. It is easy to change it back should you need too. This code is working perfectly fine and I have included a lot of functionality to allow you to play around with the file paths and file names which is why I used the dictionary for that.
The code is not commented, but if you have any questions, let me know. If you are happy with this, please mark it as a solution.
#Region "Declationss"
Dim FileDetails As New Dictionary(Of String, String)
Dim CheckedItem As Integer = 0
Dim CheckedSelected As String = ""
#End Region
#Region "Subroutines"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim result As DialogResult = FolderBrowserDialog.ShowDialog()
If Not result = Windows.Forms.DialogResult.Cancel Then
GetListOfFiles(Currentlocation:=FolderBrowserDialog.SelectedPath, NewLocation:=FolderBrowserDialog.SelectedPath & "\" & "folder", Done:=False)
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Me.CheckedItem = 0 To CheckedListBox1.CheckedItems.Count - 1
CheckedSelected = CheckedListBox1.CheckedItems(CheckedItem).ToString
If FileDetails.ContainsKey(CheckedSelected) Then
Dim zLocation As String = Nothing
zLocation = FileDetails.Item(CheckedSelected)
Dim Comparison As KeyValuePair(Of String, String)
Dim ComparisonValue As String = Nothing
Dim eFileNameOnly As String = Nothing
For Each Comparison In FileDetails
If Comparison.Key = CheckedSelected Then
ComparisonValue = Comparison.Value & Comparison.Key
eFileNameOnly = Comparison.Key
FileCopy(iLocation:=zLocation, iNewLocation:=zLocation & "folder\", eFile:=ComparisonValue, FileNameOnly:=eFileNameOnly, Done:=False)
End If
Next
End If
Next
End Sub
#End Region
#Region "Functions"
Private Function GetListOfFiles(ByVal Currentlocation As String, ByVal NewLocation As String, ByRef Done As Boolean) As String
If Directory.Exists(NewLocation) = False Then
My.Computer.FileSystem.CreateDirectory(NewLocation)
MessageBox.Show("folder created successfully, click OK")
End If
For Each eFilename As String In IO.Directory.GetFiles(Currentlocation, "*", IO.SearchOption.TopDirectoryOnly)
Dim iFileName As String = System.IO.Path.GetFileName(eFilename)
Dim NewDirNameCut As String = eFilename.Replace(iFileName, "")
FileDetails.Add(iFileName, NewDirNameCut)
CheckedListBox1.Items.Add(iFileName)
Next
Done = True
Return Done
End Function
Private Function FileCopy(ByVal iLocation As String, ByVal iNewLocation As String, ByVal eFile As String, ByVal FileNameOnly As String, ByRef Done As Boolean) As Boolean
If File.Exists(eFile) Then
Dim OldFileToCopy = iLocation & FileNameOnly
Dim NewFileToCopy = iNewLocation & FileNameOnly
If System.IO.File.Exists(OldFileToCopy) = True Then
System.IO.File.Copy(OldFileToCopy, NewFileToCopy, True)
End If
MessageBox.Show("Copied: " & iLocation & FileNameOnly & " to " & iNewLocation & FileNameOnly)
Done = True
Return Done
Else
Done = False
Return Done
End If
End Function
#End Region