Question

I've got ASP.NET intranet application written in VB. It gets a file from the user, and then depending on a few different cases it may create a few copies of the file as well as move the original.

Unfortunately I've come across a case where I get this error:

Exception Details: System.IO.IOException: The process cannot access the file 
'\\some\dir\D09_03_5_180_0.000-6.788.png' because it is being used by 
another process.

Which is thrown by My.Computer.FileSystem.CopyFile. And that's fine that it's being used by another process - it may still be saving/downloading from the user or trying to copy while another thread(?) is copying, I don't really care about that, what I want to know:

Is there any way that I can tell VB to wait to copy (also move) the file until the file is no longer in use?

Thanks

Was it helpful?

Solution

Test if the file is in use and the do what you need to do.

Public Sub WriteLogFile(ByVal pText As String, ByVal psPath As String, ByVal psName As String)

  Dim strFullFileName As String
  Dim Writer As System.IO.StreamWriter
  Dim Fs As System.IO.FileStream

  Try
     Dim DirectoryHandler As New System.IO.DirectoryInfo(psPath)
     strFullFileName = psPath & "\" & psName & Date.Today.Month.ToString & "-" & Date.Today.Day.ToString & "-" & Date.Today.Year.ToString & ".txt"
     If Not DirectoryHandler.Exists() Then
        Try
           Monitor.Enter(fsLocker)
           DirectoryHandler.Create()
        Finally
           Monitor.Exit(fsLocker)
        End Try
     End If

     Try
           If CheckIfFileIsInUse(strFullFileName) = True Then
           Thread.Sleep(500) ' wait for .5 second
           WriteLogFile(pText, psPath, psName)
           If Not Fs Is Nothing Then Fs.Close()
           If Not Writer Is Nothing Then Writer.Close()
           Exit Sub
        End If
        Monitor.Enter(fsLocker)
        Fs = New System.IO.FileStream(strFullFileName, IO.FileMode.Append, IO.FileAccess.Write, IO.FileShare.Write)
        Writer = New System.IO.StreamWriter(Fs)
        Writer.WriteLine(Date.Now.ToString & vbTab & "ProcessID: " & Process.GetCurrentProcess.Id.ToString() & vbTab & pText)
        Writer.Close()
        Fs.Close()
     Finally
        Monitor.Exit(fsLocker)
     End Try

  Catch ex As Exception

     Dim evtEMailLog As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
     evtEMailLog.Source = Process.GetCurrentProcess.ProcessName.ToString()
     evtEMailLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error)

  Finally
     If Not Fs Is Nothing Then Fs.Close()
     If Not Writer Is Nothing Then Writer.Close()
  End Try

End Sub Public Function CheckIfFileIsInUse(ByVal sFile As String) As Boolean If System.IO.File.Exists(sFile) Then Try Dim F As Short = FreeFile() FileOpen(F, sFile, OpenMode.Append, OpenAccess.Write, OpenShare.Shared) FileClose(F) Catch Return True End Try End If End Function

OTHER TIPS

Hmm... not directly. What most implementations are doing, is making a retry of copying the file, with a small timeframe (some seconds)

if you want to make a nice UI, you check via Ajax, if the copying process went well.

Well, it turns out that waiting would not work in this case:

When trying to copy a file you cannot copy a file from one location to the same location or it will throw an error (apparently). Rather than just pretending to copy the file, VB actually tries to copy the file and fails because the copy operation is trying to copy to the file it's copying from (with overwrite:=True at least).

Whoops!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top