Hey,
i'm trying to have 2 browse file dialog boxes, one that allows you to select a file from a folder, and the other folder should allow you to chose the location to copy the file to.
Thanks
Hey,
i'm trying to have 2 browse file dialog boxes, one that allows you to select a file from a folder, and the other folder should allow you to chose the location to copy the file to.
Thanks
The quickest fix for the original ask is to let the user pick the source file with an OpenFileDialog and pick a destination folder with a FolderBrowserDialog (that matches the thread title). That complements ’s save-file approach when you actually want a full filename; use FolderBrowserDialog when you want only a folder. The example below shows a safe, non-blocking copy (async), progress reporting, and a simple collision-avoidance strategy so existing files are not overwritten.
' Button click: pick source file, pick target folder, then copy with progress
Private Async Sub BtnCopy_Click(sender As Object, e As EventArgs) Handles BtnCopy.Click
Using ofd As New OpenFileDialog()
ofd.Title = "Select source file"
If ofd.ShowDialog() <> DialogResult.OK Then Return
Using fbd As New FolderBrowserDialog()
fbd.Description = "Select destination folder"
fbd.ShowNewFolderButton = True
If fbd.ShowDialog() <> DialogResult.OK Then Return
Dim progress = New Progress(Of Integer)(Sub(p) ProgressBar1.Value = Math.Min(100, p))
Dim cts = New Threading.CancellationTokenSource()
Try
Await CopyFileWithProgressAsync(ofd.FileName, fbd.SelectedPath, progress, cts.Token)
MessageBox.Show("Copy complete")
Catch ex As OperationCanceledException
MessageBox.Show("Copy cancelled")
Catch ex As Exception
MessageBox.Show("Copy failed: " & ex.Message)
End Try
End Using
End Using
End Sub
Private Async Function CopyFileWithProgressAsync(sourcePath As String, destFolder As String, progress As IProgress(Of Integer), token As Threading.CancellationToken) As Task
Dim fileName = System.IO.Path.GetFileName(sourcePath)
Dim destPath = System.IO.Path.Combine(destFolder, fileName)
destPath = GetNonConflictingPath(destPath)
Using src As New System.IO.FileStream(sourcePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read, 81920, useAsync:=True),
dst As New System.IO.FileStream(destPath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write, System.IO.FileShare.None, 81920, useAsync:=True)
Dim buffer(81919) As Byte
Dim total As Long = 0
Dim read As Integer
Do
read = Await src.ReadAsync(buffer, 0, buffer.Length, token)
If read = 0 Then Exit Do
Await dst.WriteAsync(buffer, 0, read, token)
total += read
If src.Length > 0 Then progress?.Report(CInt(total * 100L / src.Length))
Loop
End Using
End Function
Private Function GetNonConflictingPath(path As String) As String
If Not System.IO.File.Exists(path) Then Return path
Dim dir = System.IO.Path.GetDirectoryName(path)
Dim base = System.IO.Path.GetFileNameWithoutExtension(path)
Dim ext = System.IO.Path.GetExtension(path)
Dim i As Integer = 1
While True
Dim candidate = System.IO.Path.Combine(dir, String.Format("{0} ({1}){2}", base, i, ext))
If Not System.IO.File.Exists(candidate) Then Return candidate
i += 1
End While
End Function Notes and troubleshooting
Jump to Post— G_Waddell 131Hey,
i'm trying to have 2 browse file dialog boxes, one that allows you to select a file from a folder, and the other folder should allow you to chose the location to copy the file to.
Thanks
Hi You want to use an OpenFileDialog box for the Selection and …
Hey,
i'm trying to have 2 browse file dialog boxes, one that allows you to select a file from a folder, and the other folder should allow you to chose the location to copy the file to.
Thanks
Hi You want to use an OpenFileDialog box for the Selection and a SaveFileDialog box for the destination. Then you copy the source file to the destination:
Private Sub CopyFile()
dim sSource as string
dim sDestination as string
dim diaSrc as new OpenFileDialog
'set up open file
with diaSrc
.Title = "Browse to Document"
.InitialDirectory = "C:\"
.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
.RestoreDirectory = True
end with
if diaSrc.showdialog = Windows.Forms.DialogResult.OK Then
sSource = diaSrc.fileName
end if
'do the same with the destination on a savefiledialog....
'......
if file.exists(sDestination) Then
file.delete(sDestination)
end if
file.copy(sSource, sDestination)
end sub Imports System.IO
Nice Article.............
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.