Grabner

I need to determin if a file is still open before i start to read it's content.

How do i do this in Visual Baisc



Re: Visual Basic General How to determin in VB if a file is open by an other application

Anarchy

try to open it for write, if it is already opened, you'll get an exception



Re: Visual Basic General How to determin in VB if a file is open by an other application

Grabner

A little indirect, but should work. Thanks.

Anyone with a more direct way of determining if file is open by other application





Re: Visual Basic General How to determin in VB if a file is open by an other application

TaDa

That is the direct way.



Re: Visual Basic General How to determin in VB if a file is open by an other application

Anarchy

Grabner,

I've tried this before when using the filesystemwatcher which gives you an event when a files STARTS being created, but not when a file is FINISHED being created, and had the same problem. As far as I could work out, trying to open a file for write and trapping the exception is the only way to work out if a file is locked.

If you come up with a less brutal method, please let me know

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=602148&SiteID=1





Re: Visual Basic General How to determin in VB if a file is open by an other application

HappyGene

Hi,

I just tested an rtf (as a line item strip source) opened with WordPad and was able to get a binary read/write lock in VBA (Access 2K3). It waited less than a second and then gave it to me.

>>>

Open tpFullFileName For Binary Access Read Write Lock Read Write As iFileHand

<<<

I was also able to lock it with an Append access. So, in XP SP2, even the brutal isn't reliable.

** If someone knows how to defeat the file-operations negotiation, we could turn that off, brutalize the file and then turn negotiation back on. **

I saw an api version somewhere (which I need, now) and if I can find it, I'll post here. I'll bookmark this.

:) Gene






Re: Visual Basic General How to determin in VB if a file is open by an other application

HappyGene

Okeedoke, search this:

"Macro code to check whether a file is already open" for a MSN KnowledgeBase article.

It's hokkee, too. Opened it right up and got the first line easy as pie.

Still searching...






Re: Visual Basic General How to determin in VB if a file is open by an other application

HappyGene

Here's another neat util to search, but fails to give unambiguous write-lock info:

"How to Determine If a File Exists and if it is Locked by Another Process"

I didn't see a combo of flags that would certainly tell if the file was just open.

Gene






Re: Visual Basic General How to determin in VB if a file is open by an other application

HappyGene

This is cool, too, but fails; here's the search string:

Check if Excel File is Open
AND by Who

Gene






Re: Visual Basic General How to determin in VB if a file is open by an other application


Re: Visual Basic General How to determin in VB if a file is open by an other application

Anarchy

so... we still can't work out with any certainty if a file is locked

I came to this from using FileSystemWatcher, and after a CREATED event, you cannot open a file for append until it had finished being written. It is possible that WordPad is not locking the file when it is opened.

I'd still be interested in a solution to this problem, it's seems hard to believe this is so difficult!





Re: Visual Basic General How to determin in VB if a file is open by an other application

HappyGene

Anarchy,

Yes, I'm still interested, too.

That created event would be good for waiting for a file to be copied or zipped, though. I'll check that out.

Yes, none of the MS products lock the file (unless you don't want them to!!)

I agree, it shouldn't be that difficult. And it has to be available somewhere (maybe the SDK) since Russy-Poo got it to work. He must literally be a genius.

Let's keep it up,

:) Gene






Re: Visual Basic General How to determin in VB if a file is open by an other application

HappyGene

John,

I tried Mark's 'handle' and it put out some good stuff. Thanks!

But, for the file 'clip-doc.rtf' opened with wordpad.exe, it would only give the directory for clip-doc.rtf or the executable, wordpad.exe. I couldn't trap the output and determine if my current clip-doc.rtf was open, or not. It would not find 'clip' for '-p processes' or '-a handles' in general.

Bummer...

I'll keep looking,

:) Gene






Re: Visual Basic General How to determin in VB if a file is open by an other application

HappyGene

Ya know, TaskManager, see's it all, knows it all and lists most of it.

Anybody with some TaskManager style code

Thanks,

:) Gene