Click here to Skip to main content
15,881,380 members

Multiselect file in OFD and copy to new location

Luiey Ichigo asked:

Open original thread
Hi all,

I have 2 textboxes. First one for source file, second one for destination folder.

When I click source button, an openfiledialog appear for me to select file. And after I click open button, it should put the multiselect file into first textbox. Then I click select destination folder button and it will open folderbrowserdialog to select which dest. folder..but it failed to copy since it appear 1 path in the first textbox. Here are my code, please someone tell me the right code..


VB
Private Sub btnSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectFile.Click
        Dim strFileNameAndPath As String = SelectFileSource()
        txtSource.Text = strFileNameAndPath
    End Sub

    Private Sub btnSelectFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectFolder.Click
        Dim strFolderPath As String = SelectFolderDest()
        txtDest.Text = strFolderPath
    End Sub

    Private Sub btnStartCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartCopy.Click
        For Each filePath As String In txtSource.Text
            My.Computer.FileSystem.CopyFile(txtSource.Text, txtDest.Text)
        Next
    End Sub

VB
#Region "PATH FILE"
    Private Function SelectFileSource() As String
        Dim strFileName = ""
        Dim fileDialogBox As New OpenFileDialog()
        fileDialogBox.Title = "Select file to copy | " & GlobVars.strFileDesc
        fileDialogBox.Multiselect = True
        fileDialogBox.CheckPathExists = True
        fileDialogBox.Filter = "All Files (*.*)|*.*"
        fileDialogBox.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)

        If (fileDialogBox.ShowDialog() = DialogResult.OK) Then
            strFileName = fileDialogBox.FileName
        End If
        Return strFileName
    End Function

    Private Function SelectFolderDest() As String
        Dim strFolderPath = ""
        Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
        MyFolderBrowser.Description = "Select destination folder for " & GlobVars.strFileDesc
        MyFolderBrowser.RootFolder = Environment.SpecialFolder.MyComputer
        MyFolderBrowser.ShowNewFolderButton = True
        Dim dlgResult As DialogResult = MyFolderBrowser.ShowDialog()

        If dlgResult = Windows.Forms.DialogResult.OK Then
            strFolderPath = MyFolderBrowser.SelectedPath
        End If
        Return strFolderPath
    End Function
#End Region



At Button Start Copy, an exception occur "Could not complete operation since a directory already exists in this path 'C:\Program Files\Data'." in
VB
My.Computer.FileSystem.CopyFile(txtSource.Text, txtDest.Text)
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900