following code is raising exceptions.
1st exception is ok the file should not be there 2nd exception is also ok as the file either deleted or renamed or might already exist. however, i can't get rid of the third exception while creating zip file despite the fact that i use thread.sleep (thinking that the file might be still under-process) the error file is in use by another process is not resolved
' Create ZIP from "source" directory.
Debug.Print("===========Starting Process=============")
Try
Debug.Print("===========Attempt to Delete File=============")
My.Computer.FileSystem.DeleteFile("G:\abc\~tmp0000.tmp")
Catch ex As Exception
Debug.Print("===========Exception while DeleteFile=============")
Debug.Print(ex.Message)
End Try
Debug.Print("===========finshed with Delete File=============")
'Thread.Sleep(100)
Try
Debug.Print("===========Attempt to Rename File=============")
My.Computer.FileSystem.RenameFile("~tmp.0000.zip", "destination.zip")
Catch ex1 As Exception
Debug.Print("===========Exception while RenameFile=============")
Debug.Print(ex1.Message)
End Try
Debug.Print("===========finshed with Rename File=============")
Thread.Sleep(100)
Debug.Print("===========finshed with sleep=============")
Try
Debug.Print("===========Attempt to CreateFromDirectory File=============")
ZipFile.CreateFromDirectory("g:\abc", "g:\abc\~tmp0000.tmp")
Catch ex2 As Exception
Debug.Print("===========Exception while CreateFromDirectory=============")
Debug.Print(ex2.Message)
End Try
Debug.Print("===========finshed with Rename File=============")
Thread.Sleep(100)
Debug.Print("===========finshed with sleep=============")
' Extract ZIP to "destination" folder.
Try
Debug.Print("===========Attempt to Extract File=============")
ZipFile.ExtractToDirectory("g:\abc\destination.zip", "g:\abc\destination")
Catch ex2 As Exception
Debug.Print("===========Exception while ExtractToDirectory=============")
Debug.Print(ex2.Message)
End Try
Debug.Print("===========Completed Process=============")
</pre>
------------------exceptions log --------------------------------
<pre>
===========Starting Process=============
===========Attempt to Delete File=============
===========finshed with Delete File=============
===========Attempt to Rename File=============
A first chance exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.VisualBasic.dll
===========Exception while RenameFile=============
Could not find file 'g:\Debug\~tmp.0000.zip'.
===========finshed with Rename File=============
===========finshed with sleep=============
===========Attempt to CreateFromDirectory File=============
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
===========Exception while CreateFromDirectory=============
The process cannot access the file '\\..some path striped-out...\Debug\~tmp0000.tmp' because it is being used by another process.
===========finshed with Rename File=============
===========finshed with sleep=============
===========Attempt to Extract File=============
A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.IO.Compression.FileSystem.dll
===========Exception while ExtractToDirectory=============
Could not find file 'g:\abc\destination.zip'.
===========Completed Process=============
If you are trying to zip a directory take a look at this. Using your example: