Home > other >  Copy a File Using DragDrop VB.Net
Copy a File Using DragDrop VB.Net

Time:09-29

What is wrong with my code? Getting a 'Process cannot access the file because it is being used by another process' error msg Is there a way around this. My google-fu was not giving me much luck. I was not able to Move or Copy, and I will take either.

    Private Sub frmFiberTransMain_DragEnter(sender As Object, e As DragEventArgs) Handles MyBase.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
        e.Effect = DragDropEffects.All
    End If
End Sub

Private Sub frmFiberTransMain_DragDrop(sender As Object, e As DragEventArgs) Handles MyBase.DragDrop
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        Dim filePaths As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
        Call CopyFileDrop(filePaths)
    End If
End Sub

Private Sub CopyFileDrop(filePaths As String())
    For Each fileLoc As String In filePaths
        Dim fileName As String = fileLoc
        Dim fi As New IO.FileInfo(fileName)
        File.Create(fileName)
        Dim justFileName As String = fi.Name
        Dim newPathName As String = gProgDir & "\" & justFileName
        Directory.Move(fileLoc, newPathName)            
    Next fileLoc
End Sub

CodePudding user response:

File.Create(fileName) returns an open handle and you're not closing it. It doesn't look like you need that line. – @LarsTech 50 mins ago

  • Related