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

File sitting on a network drive is still open by another applicati

P: n/a
How can I detect if a file sitting on a network drive is still open by another
application?

This application resides on another machine on the network?

I am using VB.NET 2003

Your help is highly appreciated

Jun 1 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Try to open it, if you cant open it, it's still in use.
You could write a try catch around this and eventually write some loop to
test periodically if you can open it, and when you can, you could use the
file?
"Moumen VB.NET 2003/2005 Developer"
<Mo**************************@discussions.microsof t.com> wrote in message
news:1D**********************************@microsof t.com...
How can I detect if a file sitting on a network drive is still open by
another
application?

This application resides on another machine on the network?

I am using VB.NET 2003

Your help is highly appreciated

Jun 1 '06 #2

P: n/a
Hi,

I tried your approach, but with no success.

Could you please post some code on how you can do it?

Thanks
"Joris De Groote" wrote:
Try to open it, if you cant open it, it's still in use.
You could write a try catch around this and eventually write some loop to
test periodically if you can open it, and when you can, you could use the
file?
"Moumen VB.NET 2003/2005 Developer"
<Mo**************************@discussions.microsof t.com> wrote in message
news:1D**********************************@microsof t.com...
How can I detect if a file sitting on a network drive is still open by
another
application?

This application resides on another machine on the network?

I am using VB.NET 2003

Your help is highly appreciated


Jun 1 '06 #3

P: n/a
I would write something like:
' Precondition: the file "theFile" is known to exist

Public Function IsLocked(ByVal theFile As String) As Boolean

Dim theStream As FileStream
Dim theResult As Boolean

Try

' To test if the file is locked, it is enough just to try and open
it and handle
' any (resulting) exception. If an exception is raised, then the
file is locked.

theStream = New FileStream(theFile, FileMode.Open, FileAccess.Write)

' Success

theResult = False

Catch ex As IOException

' We failed to open it for writing, so it's probably locked.

theResult = True

Finally

If Not theStream Is Nothing Then
theStream.Close()
End If

End Try

Return theResult

End Function

"Moumen VB.NET 2003/2005 Developer"
<Mo**************************@discussions.microsof t.com> wrote in message
news:8F**********************************@microsof t.com...
Hi,

I tried your approach, but with no success.

Could you please post some code on how you can do it?

Thanks
"Joris De Groote" wrote:
Try to open it, if you cant open it, it's still in use.
You could write a try catch around this and eventually write some loop to
test periodically if you can open it, and when you can, you could use the
file?
"Moumen VB.NET 2003/2005 Developer"
<Mo**************************@discussions.microsof t.com> wrote in message
news:1D**********************************@microsof t.com...
> How can I detect if a file sitting on a network drive is still open by
> another
> application?
>
> This application resides on another machine on the network?
>
> I am using VB.NET 2003
>
> Your help is highly appreciated
>


Jun 1 '06 #4

P: n/a
Yep

And if you want to wait until you want to open it, you can stil write a
while loop around it and keep trying until theResult = false .

Greetz

"Robin Mark Tucker" <ro*************@removehotmail.comremove> wrote in
message news:e5*******************@news.demon.co.uk...
I would write something like:
' Precondition: the file "theFile" is known to exist

Public Function IsLocked(ByVal theFile As String) As Boolean

Dim theStream As FileStream
Dim theResult As Boolean

Try

' To test if the file is locked, it is enough just to try and open
it and handle
' any (resulting) exception. If an exception is raised, then the
file is locked.

theStream = New FileStream(theFile, FileMode.Open,
FileAccess.Write)

' Success

theResult = False

Catch ex As IOException

' We failed to open it for writing, so it's probably locked.

theResult = True

Finally

If Not theStream Is Nothing Then
theStream.Close()
End If

End Try

Return theResult

End Function

"Moumen VB.NET 2003/2005 Developer"
<Mo**************************@discussions.microsof t.com> wrote in message
news:8F**********************************@microsof t.com...
Hi,

I tried your approach, but with no success.

Could you please post some code on how you can do it?

Thanks
"Joris De Groote" wrote:
Try to open it, if you cant open it, it's still in use.
You could write a try catch around this and eventually write some loop
to
test periodically if you can open it, and when you can, you could use
the
file?
"Moumen VB.NET 2003/2005 Developer"
<Mo**************************@discussions.microsof t.com> wrote in
message
news:1D**********************************@microsof t.com...
> How can I detect if a file sitting on a network drive is still open by
> another
> application?
>
> This application resides on another machine on the network?
>
> I am using VB.NET 2003
>
> Your help is highly appreciated
>


Jun 2 '06 #5

P: n/a

Joris & Robin,

Thank you very much for your valuable replies.

However, I noticed that when I try to open a text file through NotePad.exe,
my program cannot detect that the file is open for writing by NotePad. On the
other hand, if I open the same text file with MS-Word, my program works
perfectly.

I would like my program to work with any application that tries to open the
network file.

Do you know why this happening only with NotePad?

Thanks!


"Joris De Groote" wrote:
Yep

And if you want to wait until you want to open it, you can stil write a
while loop around it and keep trying until theResult = false .

Greetz

"Robin Mark Tucker" <ro*************@removehotmail.comremove> wrote in
message news:e5*******************@news.demon.co.uk...
I would write something like:
' Precondition: the file "theFile" is known to exist

Public Function IsLocked(ByVal theFile As String) As Boolean

Dim theStream As FileStream
Dim theResult As Boolean

Try

' To test if the file is locked, it is enough just to try and open
it and handle
' any (resulting) exception. If an exception is raised, then the
file is locked.

theStream = New FileStream(theFile, FileMode.Open,
FileAccess.Write)

' Success

theResult = False

Catch ex As IOException

' We failed to open it for writing, so it's probably locked.

theResult = True

Finally

If Not theStream Is Nothing Then
theStream.Close()
End If

End Try

Return theResult

End Function

"Moumen VB.NET 2003/2005 Developer"
<Mo**************************@discussions.microsof t.com> wrote in message
news:8F**********************************@microsof t.com...
Hi,

I tried your approach, but with no success.

Could you please post some code on how you can do it?

Thanks
"Joris De Groote" wrote:

Try to open it, if you cant open it, it's still in use.
You could write a try catch around this and eventually write some loop
to
test periodically if you can open it, and when you can, you could use
the
file?
"Moumen VB.NET 2003/2005 Developer"
<Mo**************************@discussions.microsof t.com> wrote in
message
news:1D**********************************@microsof t.com...
> How can I detect if a file sitting on a network drive is still open by
> another
> application?
>
> This application resides on another machine on the network?
>
> I am using VB.NET 2003
>
> Your help is highly appreciated
>



Jun 2 '06 #6

P: n/a
I don't know this for sure (but this is what I think)
I don't think Notepad locks a file (since its pretty much a very basic
program), MS-Word does.

Joris

"Moumen VB.NET 2003/2005 Developer"
<Mo**************************@discussions.microsof t.com> wrote in message
news:B1**********************************@microsof t.com...

Joris & Robin,

Thank you very much for your valuable replies.

However, I noticed that when I try to open a text file through
NotePad.exe,
my program cannot detect that the file is open for writing by NotePad. On
the
other hand, if I open the same text file with MS-Word, my program works
perfectly.

I would like my program to work with any application that tries to open
the
network file.

Do you know why this happening only with NotePad?

Thanks!


"Joris De Groote" wrote:
Yep

And if you want to wait until you want to open it, you can stil write a
while loop around it and keep trying until theResult = false .

Greetz

"Robin Mark Tucker" <ro*************@removehotmail.comremove> wrote in
message news:e5*******************@news.demon.co.uk...
>I would write something like:
>
>
> ' Precondition: the file "theFile" is known to exist
>
> Public Function IsLocked(ByVal theFile As String) As Boolean
>
> Dim theStream As FileStream
> Dim theResult As Boolean
>
> Try
>
> ' To test if the file is locked, it is enough just to try and
> open
> it and handle
> ' any (resulting) exception. If an exception is raised, then the
> file is locked.
>
> theStream = New FileStream(theFile, FileMode.Open,
> FileAccess.Write)
>
> ' Success
>
> theResult = False
>
> Catch ex As IOException
>
> ' We failed to open it for writing, so it's probably locked.
>
> theResult = True
>
> Finally
>
> If Not theStream Is Nothing Then
> theStream.Close()
> End If
>
> End Try
>
> Return theResult
>
> End Function
>
>
>
>
>
> "Moumen VB.NET 2003/2005 Developer"
> <Mo**************************@discussions.microsof t.com> wrote in
> message
> news:8F**********************************@microsof t.com...
>> Hi,
>>
>> I tried your approach, but with no success.
>>
>> Could you please post some code on how you can do it?
>>
>> Thanks
>>
>>
>> "Joris De Groote" wrote:
>>
>>> Try to open it, if you cant open it, it's still in use.
>>> You could write a try catch around this and eventually write some
>>> loop
>>> to
>>> test periodically if you can open it, and when you can, you could use
>>> the
>>> file?
>>>
>>>
>>> "Moumen VB.NET 2003/2005 Developer"
>>> <Mo**************************@discussions.microsof t.com> wrote in
>>> message
>>> news:1D**********************************@microsof t.com...
>>> > How can I detect if a file sitting on a network drive is still open
>>> > by
>>> > another
>>> > application?
>>> >
>>> > This application resides on another machine on the network?
>>> >
>>> > I am using VB.NET 2003
>>> >
>>> > Your help is highly appreciated
>>> >
>>>
>>>
>>>
>
>


Jun 6 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.