.NET

Reply
*pkirill
Message 1 of 3 (143 Views)

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

143 Views, 2 Replies
10-18-2006 06:49 AM
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!
Distinguished Contributor
Mikko
Posts: 212
Registered: ‎10-03-2003
Message 2 of 3 (143 Views)

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

10-18-2006 08:43 AM in reply to: *pkirill
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
Distinguished Contributor
cgay
Posts: 125
Registered: ‎08-02-2005
Message 3 of 3 (143 Views)

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

10-18-2006 11:07 AM in reply to: *pkirill
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
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.