Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Watch file


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Jan 7, 2016 10:35 AM Last Post By: Remy Lebeau (Te...
Martin Masci

Posts: 29
Registered: 4/3/04
Watch file  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2016 6:33 AM
My application use a database. This database can be located on hard disk, on usb pen drive or on other computer (the same LAN).
I need watch the database file to know if it is no longer accessible. For example the user remove the usb pen drive orb there are problem on LAN.

Is there a simple and nonintrusive way to do it to prevent error?
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Watch file  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2016 10:01 AM   in response to: Martin Masci in response to: Martin Masci
Martin Masci wrote:

My application use a database. This database can be located on hard
disk, on usb pen drive or on other computer (the same LAN). I need
watch the database file to know if it is no longer accessible. For
example the user remove the usb pen drive orb there are problem on
LAN.

Is there a simple and nonintrusive way to do it to prevent error?

I hope for you that the database is read-only for your program. Having
a writable database on a removable medium is a recipe for database
corruption.

What kind of data access framework are you using here? They usally have
an event that gets fired on database errors, and that is the best place
to detect and deal with access problems.


--
Peter Below
TeamB

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Watch file  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2016 10:35 AM   in response to: Martin Masci in response to: Martin Masci
Martin wrote:

I need watch the database file to know if it is no longer accessible.
For example the user remove the usb pen drive orb there are problem on
LAN.

You can't really watch the database file itself, you would have to poll the
file system periodically to see if the file still exists. Although you can
register to receive events from the OS when something changes on the file
system, like a file/folder being added/deleted.

For a USB device, you can monitor and respond to WM_DEVICECHANGE window messages:

WM_DEVICECHANGE message
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363480.aspx

Detecting Media Insertion or Removal
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363215.aspx

Processing a Request to Remove a Device
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363427.aspx

Device Events
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363217.aspx

Of course, there is nothing to stop a user from simply pulling out the device
without asking Windows to "eject" it nicely first. You should still receive
some of the events, though.

For a LAN connection, you can monitor the LAN adapter to detect when it connects
to, and disconnects from, the LAN. On Vista+, you can register to receive
events when the LAN connection changes state:

About the Network List Manager API
https://msdn.microsoft.com/en-us/library/aa965303.aspx

On older systems, you can use things like GetAdaptersInfo() or GetIpAddrTable()
to poll the LAN adapter periodically for its current state.

Of course, either way, it is possible for network errors to occur and disrupt
your database's connection without losing the LAN connection itself.

Is there a simple and nonintrusive way to do it to prevent error?

No, and you should not be trying to avoid it. Use the database normally,
and let the database driver manage the database access for you. If an error
occurs, it will tell you in some manner (event, exception, etc). React accordingly.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02