.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

2 REPLIES 2
Reply
Message 1 of 3
Anonymous
783 Views, 2 Replies

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

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!
2 REPLIES 2
Message 2 of 3
Mikko
in reply to: Anonymous

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
Message 3 of 3
cgay
in reply to: Anonymous

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost