By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

Telling if an app has a file locked

P: n/a
I need a way to tell if a program such as word or excel has a document it
locked (open for editing). Is there any way to do this in VB.NET? All I need
is a its open for editing or not response thats all... thanks!
Apr 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I guess one way to do it is like this, is this the best way though?

''' <summary>

''' Check if a file is locked or open for editing

''' </summary>

''' <param name="filePath"></param>

''' <returns></returns>

''' <remarks></remarks>

Public Shared Function ISFileLockedForWriting(ByVal filePath As String) As
Boolean

Dim result As Boolean = False

Try

Using fs As System.IO.Stream = IO.File.Open(filePath, IO.FileMode.Open,
IO.FileAccess.Write, IO.FileShare.None)

fs.Close()

result = False

End Using

Catch ex As Exception

result = True

End Try

Return result

End Function

"Smokey Grindle" <no****@dontspamme.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>I need a way to tell if a program such as word or excel has a document it
locked (open for editing). Is there any way to do this in VB.NET? All I
need is a its open for editing or not response thats all... thanks!

Apr 4 '07 #2

P: n/a
afaik its the only way without delving deep into the guts of windoze. Its a
pain when I'm debugging, because a perfectly valid program action has to
result in a thrown exception but you know, can't have everything (like a
simple locked status bit for instance).

One thing I would note, you should be careful you know exactly why it failed
in the exception handler (handle IOException instead) - it might fail due to
filenotfound (a bug for instance) or some other reason not related to
locking.
Apr 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.