title: Checking to see if file is open and If so write the error to the access database
body: <p>I wrote a method to check to see if a files is open and if it is open write the error to an access database. I have the file open, it reads the try but never gets to the catch ex as exception so that it writes to the database. It goes back to my copy method that I have created and goes to the catch exception there where it sees that the files is open.</p>
  
  <pre><code>  Private Sub IsFileOpen(nFile As String)
          Dim stream As FileStream
          Dim sqlError As New OleDb.OleDbCommand
          sqlError = conn.CreateCommand
  
          Dim docName As String = System.IO.Path.GetFileNameWithoutExtension(nFile)
          Dim fileName As String = System.IO.Path.GetFileName(nFile)
          Dim processDate As String = Date.Now
          Dim errorNum As String = ""
  
          Try
              stream = File.Open(nFile, FileMode.Open, FileAccess.Read)
              stream.Close()
          Catch ex As Exception
              If TypeOf ex Is Exception Then
                  errorNum = ex.Message
                  sqlError.CommandText = "INSERT INTO tblDocumentCopyError ([DocName], [FileName], [ProcessDate], [ErrorNumber]) " _
                                      &amp; " VALUES (@DocName, @FileName, @ProcessDate, @ErrorNumber) "
  
                  sqlError.Parameters.AddWithValue("@DocName", docName)
                  sqlError.Parameters.AddWithValue("@FileName", fileName)
                  sqlError.Parameters.AddWithValue("@ProcessDate", processDate)
                  sqlError.Parameters.AddWithValue("@ErrorNumber", errorNum)
                  sqlError.ExecuteNonQuery()
              Else
                  MessageBox.Show(vbCrLf &amp; ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
              End If
          End Try
  
      End Sub
  </code></pre>