如何使用vb.net中的openfiledialog将文件复制到新目录?

时间:2015-03-06 03:34:06

标签: vb.net openfiledialog

我需要将使用OpenFileDialog选择的mp3文件复制到新目录。问题是文件夹中的所有文件都复制到新目录。我只想复制在该文件夹中选择的特定文件。请帮助。

注意:打开mp3文件后,Textbox1将显示文件的源路径(sourcepath)。然后Textbox2将在点击保存按钮后显示新路径(destPath)。

 Private Shared Sub CopyDirectory(ByVal sourcePath As String, ByVal destPath As String)
    If Not Directory.Exists(destPath) Then
        Directory.CreateDirectory(destPath)
    End If

    For Each file__1 As String In Directory.GetFiles(Path.GetDirectoryName(sourcePath))
        Dim dest As String = Path.Combine(destPath, Path.GetFileName(file__1))
        File.Copy(file__1, dest)
    Next

    For Each folder As String In Directory.GetDirectories(Path.GetDirectoryName(sourcePath))
        Dim dest As String = Path.Combine(destPath, Path.GetFileName(folder))
        CopyDirectory(folder, dest)
    Next
End Sub

 Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
    Dim sourcepath As String = ""
    Dim DestPath As String = "C:\Users\Big Boss Eis\Desktop\vb file maintenance\fileMaintenance with database\fileMaintenance"
    sourcepath = TextBox1.Text
    CopyDirectory(sourcepath, DestPath)
End Sub

 Private Sub OpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFile.Click
    If (OpenFileDialog1.ShowDialog = DialogResult.OK) Then
   AxWindowsMediaPlayer1.URL() = OpenFileDialog1.FileName
        TextBox1.Text = OpenFileDialog1.FileName
   End If
 End Sub

1 个答案:

答案 0 :(得分:0)

用此

替换整个CopyDirectory函数
Private Shared Sub CopyDirectory(ByVal sourcePath As String, ByVal destPath As String)
    If System.IO.File.Exists( sourcePath ) = True Then

        System.IO.File.Copy( sourcePath , destPath )
        MsgBox("File Copied")

    End If
End Sub

通过手机编辑,原谅任何错误