Private Sub IsFileOpen(ByVal file As FileInfo)
Dim stream As FileStream = Nothing
Try
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
stream.Close()
Catch ex As Exception
If TypeOf ex Is IOException AndAlso IsFileLocked(ex) Then
' do something here, either close the file if you have a handle, show a msgbox, retry or as a last resort terminate the process - which could cause corruption and lose data
End If
End Try
End Sub
Private Shared Function IsFileLocked(exception As Exception) As Boolean
Dim errorCode As Integer = Marshal.GetHRForException(exception) And ((1 << 16) - 1)
Return errorCode = 32 OrElse errorCode = 33
End Function
File.GetLastAccessTime() 처리 가능성은? –
파일이 열리는 것을 감지하는 직접적인 방법은 없습니다. – coder
이것은 내가 질문 한 내용입니다. http://stackoverflow.com/questions/6706312/how-to-check-if-the-text-file-is-open-and-close-the-text-file 답변을 얻었습니다. – coder