Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Xceed.Zip.ZipEvents

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #19574 |

    Hi trying to catch exceptions when a user attempts to zip a file. Main goal is to alert the user when a file is open. Code is below using VB 2005. Is there a better way. Also can I use a UserData value ?
    Private Sub CheckInButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckInButton.Click
    Xceed.Zip.Licenser.LicenseKey = “ZIN23-#####-#####-####”
    tmpCADFile = “jeb”
    Dim cadfile As New DiskFile(“\\172.16.104.6\vol1\drawings\” & tmpCADFile & “.dwg”)
    Dim zEvents As New Xceed.Zip.ZipEvents
    AddHandler zEvents.ItemException, AddressOf OnItemException
    Try
    ‘name of zip file
    Dim zip As New ZipArchive(New DiskFile(“\\172.16.104.6\vol1\apps\nclib\” & ZipFileName & “.zip”))
    cadfile.CopyTo(zEvents, Nothing, zip, True)
    MessageBox.Show(“Successfully compressed ” & tmpCADFile, “DWG Zipped”, MessageBoxButtons.OK, MessageBoxIcon.Information)
    Catch ex As Exception
    Select Case RetVal
    Case 9
    MessageBox.Show(“Drawing file is still open, please close it and check-in.”, “Compression Sample”, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
    Case 10
    MessageBox.Show(“There was an error compressing the file” & vbCrLf & ex.Message, “Compression Sample”, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
    End Select
    End Try
    End Sub

    Public Sub OnItemException(ByVal sender As Object, ByVal e As ItemExceptionEventArgs)
    RetVal = e.Action.GetTypeCode
    End Sub

    Imported from legacy forums. Posted by mike (had 6386 views)

    User (Old forums)
    Member
    Post count: 23064

    In the OnItemException method, you’ll probably want to pop a message box to the user saying “Can’t read file xyz”, and ask if the user wish to reply (i.e. he’ll close the other app locking that file) or ignore (skip that file, but continue with the next) or abort (make CopyTo throw the same exception , e.Exception). In short, it’s up to you to set e.Action to one of the three ItemExceptionAction values. And all this can be done conditional to the type of exception available in e.Exception.

    Imported from legacy forums. Posted by Martin (had 466 views)

    User (Old forums)
    Member
    Post count: 23064

    Martin thanks for the reply, but I have a question. New to all this event/exception throwing. Right now i do not have a message box in the exception. The copyto throws the exception, goes to the On ItemException routine and then comes back to the button click and then falls into my catch. Do I have the code right? Also you say make CopyTo throw the same exception, isn’t that what Im doing? if not what do I need to do?

    Imported from legacy forums. Posted by mike (had 604 views)

    User (Old forums)
    Member
    Post count: 23064

    Martin just a quick update, I chnaged my code to;
    Try
    ‘name of zip file
    Dim zip As New ZipArchive(New DiskFile(“\\172.16.104.6\vol1\apps\nclib\” & ZipFileName & “.zip”))
    cadfile.CopyTo(zEvents, Nothing, zip, True)
    MessageBox.Show(“Successfully compressed ” & tmpCADFile, “DWG Zipped”, MessageBoxButtons.OK, MessageBoxIcon.Information)
    Catch ex As Exception
    MessageBox.Show(“There was an error compressing the file” & vbCrLf & ex.Message, “Compression Sample”, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub

    End Try
    also changed to;
    Public Sub OnItemException(ByVal sender As Object, ByVal e As ItemExceptionEventArgs)
    If MessageBox.Show(“Drawing file is opened, please close it add then click the button”, “File in use”, MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
    e.Action = ItemExceptionAction.Retry
    Else
    e.Action = ItemExceptionAction.Abort
    End If

    End Sub
    Like you said the it allows the user to close the open file, click OK the the copyto completes. Right now if they click ‘Cancel’ it falls into the catch. Thick this is a good idea, or should I try to exit the calling sub, if possible, from OnItemException if they cancel?

    Imported from legacy forums. Posted by mike (had 556 views)

    User (Old forums)
    Member
    Post count: 23064

    martin, ok 1 more until you answer. Is there a way to tell in the itemexception sub what exception caused the copyto to throw the exception. In particular i wold like to know if it’s a file open condition, or a file that doesn’t exist. That way I could pop a message box with related wording.

    Imported from legacy forums. Posted by mike (had 287 views)

    User (Old forums)
    Member
    Post count: 23064

    The default value of e.Action is ItemExceptionAction.Abort. That’s why in your first piece of code, CopyTo ended throwing. Your updated code looks correct, and does what it stands for: give the user a chance to close a file. Finally, you can adapt your code based on the exception like this:

    <code>
    Public Sub OnItemException(ByVal sender As Object, ByVal e As ItemExceptionEventArgs)
    If TypeOf e.Exception Is FileSystemIOException Then
    If MessageBox.Show(“Drawing file is opened, please close it add then click the button”, _
    “File in use”, MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
    e.Action = ItemExceptionAction.Retry
    Else
    e.Action = ItemExceptionAction.Abort
    End If
    ElseIf TypeOf e.Exception Is ItemDoesNotExistException Then

    End If
    End Sub
    </code>

    Now, I just realized you are copying a single file (i.e. “cadfile” is a DiskFile). The whole ItemException event is nice for handling multiples files copied from a folder, and works well in your case too. But you may have simpler code if you forget about the ZipEvents, and simply loop on “cadfile.CopyTo(zip, True)” until it does not throw an exception. Your call!

    Imported from legacy forums. Posted by Martin (had 405 views)

    User (Old forums)
    Member
    Post count: 23064

    Used the sugested code for OnItem as follows, the only problem is when the files does not exist and the user clicks ok to abort it returns back to my “main” sub and falls into the catch, whic shows another message box. if i don’t use a try/catch i get an unhandled exception when it returns from the .Abort. is there something I’m missing. What do you mean by loop until no exceptions, don’t I need zipevents to see the exceptions?
    Public Sub OnItemException(ByVal sender As Object, ByVal e As ItemExceptionEventArgs)
    If TypeOf e.Exception Is FileSystemIOException Then
    If MessageBox.Show(“Drawing file is opened, please close it add then click the button”, “File in use”, MessageBoxButtons.OK) = Windows.Forms.DialogResult.OK Then
    e.Action = ItemExceptionAction.Retry
    End If
    ElseIf TypeOf e.Exception Is ItemDoesNotExistException Then
    If MessageBox.Show(“Drawing file not found, check-in will abort”, “File in use”, MessageBoxButtons.OK) = Windows.Forms.DialogResult.OK Then
    e.Action = ItemExceptionAction.Abort
    End If
    End If
    End Sub

    Imported from legacy forums. Posted by mike (had 398 views)

    User (Old forums)
    Member
    Post count: 23064

    I’ll answer this last question, but I beleive, for <b>your</b> best interest, that reading a little bit more about VB.NET could help you understand better what are exceptions, and how you handle them. I hope I’m not being rude, it’s not at all my intentions, only an “advise”. 🙂

    Now, first, about exceptions: The “Catch” part of a Try/Catch block does not have to contain code. It can be empty. In that case, the exception is swallowed by your code, without any signs it ever occured. Furthermore, there can be more than one Catch section in a Try/Catch block.

    Second, don’t call “cadfile.CopyTo” if you know the file does not exist. How do you know? Just check if its “Exists” property is True.

    Finally, when I say “loop on CopyTo”, I’m thinking about this:

    Dim loopOnCopy As Boolean = False

    Do
    Try
    loopOnCopy = False
    cadfile.CopyTo(zip, True)
    Catch except As FileSystemIOException
    If MessageBox.Show(“The file is in use…”, …) = DialogResult.OK Then
    loopOnCopy = True
    End If
    Catch except As Exception
    MessageBox.Show(“An error occured…”, …)
    ‘ You can use except.message in your message if you wish.
    End Try
    Loop While loopOnCopy

    If you stick with the ItemException events (which is only an event proper to Xceed Zip for .NET, saying “The method you called is about to throw an exception. Do you want to prevent that exception from being throw?), then simply remove any MessageBox from OnItemException if the exception isn’t of type “FileSystemIOException”, and leave the job of displaying a message to your Catch block around “CopyTo”.

    Hope this helps.

    Imported from legacy forums. Posted by Martin (had 494 views)

    User (Old forums)
    Member
    Post count: 23064

    Ok thanks for the help. I understand your advice and will take into consideration. I use try/catch to catch exceptions from database related all the time. Just wasn’t sure about the exceptions that Xceed throws and couldn’t find any good decsriptions in the docs. The reason for checking if file exists before zipping is we have had CAD programmers work on a drawing, use it to generate a machine ready NC file and delete it before checking the programs into the database and zip files that store thousands of files. The other issue is this app is being updated from vb 6 that used your xceed control from around 96.
    Thanks again for all the help, Martin!

    Imported from legacy forums. Posted by mike (had 6757 views)

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.