Check if drawing is open by another user *before opening*? VB.NET

Check if drawing is open by another user *before opening*? VB.NET

Anonymous
Not applicable
1,096 Views
2 Replies
Message 1 of 3

Check if drawing is open by another user *before opening*? VB.NET

Anonymous
Not applicable
Is there a way to check if a file is open by another user before you open
it? I know once a file is open, I can use the ThisDrawing.ReadOnly property
to check it. I was just trying to save some time by not having to open a
drawing before processing it.

There doesn't seem to be a property under System.IO.File to handle this.
I've tried this code:
Public Shared Function IsFileOpen(ByVal filename As String) As Boolean
Try
System.IO.File.Open(filename, IO.FileMode.Open,IO.FileAccess.Read,
IO.FileShare.None)
FileClose(1)
Return False
Catch ex As Exception
Return True
End Try
End Function

But it seems to change the properies of the drawing so AutoCAD won't open
it. Any help would be appreciated!
0 Likes
1,097 Views
2 Replies
Replies (2)
Message 2 of 3

Mikko
Advocate
Advocate
Looks like you need to set your opened file to an object before you can close it.

Function IsFileOpen(ByVal FileName As String) As Boolean
Dim FileTest As Object
Try
FileTest = System.IO.File.Open(FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
IsFileOpen = False
FileTest.Close()
Catch ex As Exception
IsFileOpen = True
End Try
End Function
0 Likes
Message 3 of 3

cgay
Enthusiast
Enthusiast
VB.Net 2005:

Function FileInUse(ByVal FN As String) As Boolean
Try
Using fs As IO.FileStream = IO.File.Open(FN, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
Return False
End Using
Catch ex As IO.IOException
Return True
End Try
End Function

<= VB.NET 2003:

Function FileInUse(ByVal FN As String) As Boolean
Dim fs As IO.FileStream = Nothing
Try
fs = IO.File.Open(FN, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
Return False
Catch ex As IO.IOException
Return True
Finally
If Not fs Is Nothing Then
fs.Close()
End If
End Try
End Function

C
0 Likes