Delphi notification when a file gets updated
Asked Answered
Q

4

10

My app contains documents in its database. The users can open the documents in which case, the document gets saved to a temporary folder and gets opened on the user's computer.

I'd like to get a notification when one of these temporary files are changed, and offer the user to save the changed document back to the database.

What is the most simple way to do this in Delphi7? (I suppose it requires some shell magic or 3rd party component)

Thanks!

Quoits answered 5/8, 2010 at 19:45 Comment(1)
Where are the documents loaded? TRichEdit? TMemo?Bailar
G
9

You can either:

  1. use the Win32 API SHChangeNotifyRegister function to watch for changes in the temp folder, and then have your callback check if your temporary files are reporting changes.

  2. since you know the exact file(s) you are interested in, you can manually monitor them directly for changes to their sizes and timestamps using FindFirstFile in a timer or thread.

Ghislainegholston answered 5/8, 2010 at 20:25 Comment(0)
S
11

You can detect changes in your temporary files (or any file) using the TJvChangeNotify component from the JEDI JVCL collection.

Sweepings answered 5/8, 2010 at 19:51 Comment(1)
I prefer not to use any JVCL components since I've found them to be kind of buggy. Not to talk about having to install gazilion components just to have the one you actually need...Quoits
G
9

You can either:

  1. use the Win32 API SHChangeNotifyRegister function to watch for changes in the temp folder, and then have your callback check if your temporary files are reporting changes.

  2. since you know the exact file(s) you are interested in, you can manually monitor them directly for changes to their sizes and timestamps using FindFirstFile in a timer or thread.

Ghislainegholston answered 5/8, 2010 at 20:25 Comment(0)
H
6

In addition to what RRuz and Remy Lebeau wrote:

Note that TJvChangeNotify in the JvChangeNotify unit makes use of the FindFirstChangeNotification API call; this is the MSDN documentation. Note it is a bit counter-intuitive: see the thread mentioned below on how to use it inside a while loop.

There is also the ReadDirectoryChanges API call, which is not wrapped by the JCL/JVCL, and has MSDN documentation here and there is a Delphi win32 example as well.

This thread explains the differences between the two API calls.

--jeroen

Highness answered 6/8, 2010 at 8:21 Comment(1)
Thanks for the info, gonna check these out as well.Quoits
B
0

Here's a simple way to use the complicated TJvChangeNotify component of the JEDI JVCL to monitor a directory.

  1. Drop a jvChangeNotify component on your main form.
  2. Click its Notifications property.
  3. In the Editing JvChangeNotify1.Notifications dialog, click the Add New icon.
  4. On the Object Inspector, checkmark the events you want to monitor.
  5. Close the dialog.
  6. On the Object Inspector Events tab, double-click OnChangeNotify.
  7. Write code for what you want to do when the folder changes.

In your main form Create procedure, add this code:

var rJvChangeItem:= JvChangeNotify1.Notifications[0];
rJvChangeItem.Directory:= [the folder you want to monitor - string];
JvChangeNotify1.Active:= True;
Botswana answered 19/4, 2022 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.