John Kurlak
John Kurlak

Reputation: 6680

VB.NET: Detect if a text file is open

Is there any way to determine if a text file is currently open in a text editor? Or better yet, is there a way to trigger an event when a text file is opened (from any program)?

Upvotes: 0

Views: 5632

Answers (3)

hova
hova

Reputation: 2841

When most editors have a file open they typically follow a set strategy:
1. Open File
2.Read entire contents into buffer
3.Close File

Then your program runs. Since the file is already closed, any attempts to open it will of course succeed. Using a FileSystemWatcher this can be detected if a file is open or closed. However, you will not be able to detect if the file has been open prior to your program running.

Upvotes: 1

Tom Anderson
Tom Anderson

Reputation: 10827

Using the FileSystemWatcher component you can detect Changed, Created, Deleted, and Renamed events statically.

If you want to detect Last Access you need to manually set the NotifyFilter to include LastAccess.

Upvotes: 5

John Kurlak
John Kurlak

Reputation: 6680

I think I'll make a Timer that checks the last modified time of the file.... and when the program starts, it'll get the last modified time.

Upvotes: -1

Related Questions