Sitten Thanks for the response,
below is a little program i created to generate the error:
It just needs one Button on a form and System.IO.Compression and System.IO.Compression.FileSystem added as references.
Code:
Imports System.Windows.Forms
Imports System
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System.IO
Imports System.IO.Compression
Imports System.IO.DriveInfo
Imports System.Management
Imports System.Xml
Public Class Form1
Private bw As BackgroundWorker = New BackgroundWorker
Dim SaveLoc As String = "E:\"
Dim startedCompress As Boolean = False
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
bw.RunWorkerAsync()
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
Debug.Print(DateTime.Now.ToString & " - BW Started")
Try
If bw.CancellationPending = True Then
e.Cancel = True
Else
'do work
doCompress("C:\Temp\Test")
End If
Catch ex As Exception
Debug.Print(DateTime.Now.ToString & " - Unable to start background worker thread: " & ex.Message)
End Try
End Sub
Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
Debug.Print(DateTime.Now.ToString & " - BW Complete")
End Sub
Private Sub doCompress(ByVal fn As String)
Try
If Directory.Exists(Mid$(SaveLoc, 1, 3)) Then
startedCompress = True
If Not (fn.Contains("TestAudio")) And fn <> "" Then
Dim fileName As String = System.IO.Path.GetFileName(fn)
Dim dirname As String = DateTime.Now.ToString("yyyyMMdd-HHmmss")
If My.Computer.FileSystem.DirectoryExists(SaveLoc & "\" & dirname) = False Then
My.Computer.FileSystem.CreateDirectory(SaveLoc & "\" & dirname)
End If
Dim wavCompressname = "SND-" & fileName & ".zip"
Dim wavName = fileName & ".wav"
'Do Audio
If System.IO.File.Exists(SaveLoc & wavCompressname) Then
System.IO.File.Delete(SaveLoc & wavCompressname)
End If
Dim audDir As String = fn & "\"
Dim ZipDir As String = SaveLoc & dirname & "\" & wavCompressname
' Debug.Print("-------------------------------------------------" & fileName & "--------------------------")
'Debug.Print(DateTime.Now.ToString & " - Start Compress")
If File.Exists(ZipDir) Then
ZipDir = ZipDir.Replace(".zip", "-1.zip")
' Debug.Print("file exists - renameing file to: " & ZipDir)
End If
ZipFile.CreateFromDirectory(audDir, ZipDir, CompressionLevel.Optimal, False)
startedCompress = False
End If
End If
Catch dirNotFound As System.IO.DirectoryNotFoundException
' Code to handle DirectoryNotFoundException.
Debug.Print(DateTime.Now.ToString & " - Directory Not Found: ")
Catch fileNotFound As System.IO.FileNotFoundException
' Code to handle FileNotFoundException.
Debug.Print(DateTime.Now.ToString & " - File Not Found: ")
Catch *********** As System.IO.***********Exception
' Code to handle ***********Exception.
Debug.Print(DateTime.Now.ToString & " - Path Too Long: ")
Catch ioEx As System.IO.IOException
' Code to handle IOException.
Debug.Print(DateTime.Now.ToString & " - IO: ")
Catch security As System.Security.SecurityException
' Code to handle SecurityException.
Debug.Print(DateTime.Now.ToString & " - Security: ")
Catch ex As Exception
Debug.Print(DateTime.Now.ToString & " - Could not Compress files: " & ex.Message)
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
bw.WorkerSupportsCancellation = True
bw.WorkerReportsProgress = False
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
End Sub
End Class
I put some stuff into the
"C:\Temp\Test" directory, this will be zipped onto the flash drive located in the SaveLoc variable in this case "E:\", make sure there is enough in the temp\test directory to take a little time to compress, i have about 23MB of images and audio this takes about 4-6 seconds to compress and save the files.
Once you click the button wait a second and then remove the USB flash drive (please use a clean drive as i would hate for anything you have on the drive becoming corrupt due to bad removal). I have had to do this 3 or 4 times before it shows the error below:
Attachment 139385
when i look at the View Details i get:
Attachment 139387
The image above i cant seem to find a way to save all of the information in an XML or text format from VS2012
the immediate window shows:
the actual exception occurred after the Background worker reported complete, so i am at a loss as i would expect it to do the same each time, but as you see from above it reported it was caught when it gave the time and the - IO text, but sometimes it crashes.
I have no idea what to try next.
Hopefully this extra info helps explain why i am confused.
Thanks
Stu