【问题标题】:how to copy file to new directory using openfiledialog in vb.net?如何使用 vb.net 中的 openfiledialog 将文件复制到新目录?
【发布时间】:2015-05-07 15:40:10
【问题描述】:

我需要将使用 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

【问题讨论】:

    标签: vb.net openfiledialog


    【解决方案1】:

    用这个替换你的整个 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
    

    手机编辑,如有错误请见谅

    【讨论】:

    • FileToCopy 的值是多少?
    • 请为您的答案添加一些解释。
    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 2012-03-18
    • 2019-06-21
    • 2014-04-30
    • 1970-01-01
    • 2019-05-03
    • 2019-11-14
    • 2014-11-14
    相关资源
    最近更新 更多