locked
VB2005: IO.File.Delete not catching exceptions when file does not exist RRS feed

  • Question

  • Greetings,

    I'm writing a SSIS 2005 script (vb.net) in which I want the functionality to checks if a file exists if so then delete it.

    For testing purposes I'm renaming files to replicate an exception i.e. file cannot be read for whatever circumstance, to ensure the error handling is correct.

    The code works fine and catches any errors with the  File.Exists(errorLookUpLogFile) command, if the file does not exist/cannot be read but not with the code below using System.IO.File.Delete(lookupErrorLogFile) as when file is renamed and therefore cannot be deleted (looking for the file under it's original name then no exception is shown (see full code below, have yet to try catching specific exception for if a files doesnt exist)
     

      'Loop through the attempted retry count to remove the errorlookuplogfile
       For retryLoop = 1 To retryAttempts
    
        _eventLog.WriteInformationEvent("Attempt " & retryLoop & " of " & retryAttempts & " deleting " & lookupErrorLogFile & ".")
    
        Try
         System.IO.File.Delete(lookupErrorLogFile)
         errorLookUpErrorLogFileDeleted = True
         Return errorLookUpErrorLogFileDeleted
    
        Catch ex As Exception
    
         'Result unsuccessful, record result in package execution log file
         _eventLog.WriteInformationEvent("Attempt " & retryLoop & " of " & retryAttempts & ": deleting lookup error file " & lookupErrorLogFile & ".", Galleria.SSIS.SSISEventLogProvider.EventType.Warning)
    
         'Wait for the specified time before attempting the next check
         System.Threading.Thread.Sleep(retryDelay)
    
        End Try
    
       Next
    

    Any ideas why no exception is occuring?

    Thanks

    Rob


    Tuesday, August 30, 2011 10:08 PM

Answers

  • Ahh just found this  http://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.90).aspx which details an exception is not thrown if the specified file does not exist.

    Investigating what can be doen, as say a network issue may prevent the file from being deleting and I wouldnt want the script to record the transaction as successful, perhaps use the check file exists function again?

     

    • Proposed as answer by Murugan A Wednesday, August 31, 2011 8:35 AM
    • Marked as answer by Liliane Teng Friday, September 9, 2011 7:04 AM
    Tuesday, August 30, 2011 10:44 PM

All replies

  • Ahh just found this  http://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.90).aspx which details an exception is not thrown if the specified file does not exist.

    Investigating what can be doen, as say a network issue may prevent the file from being deleting and I wouldnt want the script to record the transaction as successful, perhaps use the check file exists function again?

     

    • Proposed as answer by Murugan A Wednesday, August 31, 2011 8:35 AM
    • Marked as answer by Liliane Teng Friday, September 9, 2011 7:04 AM
    Tuesday, August 30, 2011 10:44 PM
  • Hi Robtyketto,

    Glad to see you have found helpful information and thanks for your sharing. If you need further assistance about this issue in future, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 9, 2011 7:07 AM