In Delphi, you can use the Windows Shell API to receive notifications of shell changes such as file creation, deletion, and modification. Here is an example of how to use the Shell API to receive shell change notifications in Delphi:
- Add the ShlObj, ActiveX, and ComObj units to your uses clause.
- Create a form with a TTimer component, and set the Interval property to a value that represents the frequency of the check, for example, 1000 (1 second).
- In the OnTimer event of the TTimer, use the SHChangeNotifyRegister function to register for shell change notifications.
- In the OnTimer event, pass the handle of the form as the first parameter, and SHCNE_ALLEVENTS as the second parameter to receive notifications for all shell events.
- In the OnChange event of the form, use the SHChangeNotifyGetDetails function to get detailed information about the change that occurred.
- Use a case statement to check for specific change events and take appropriate actions.
- In the OnDestroy event of the form, use the SHChangeNotifyDeregister function to unregister for shell change notifications.
Here is an example of how to use the SHChangeNotifyRegister function to register for shell change notifications:
1 2 3 4 |
procedure TForm1.Timer1Timer(Sender: TObject); begin SHChangeNotifyRegister(Handle, SHCNRF_NewDelivery, SHCNE_ALLEVENTS, WM_SHELLNOTIFY, 1, nil); end; |
Here is an example of how to use the SHChangeNotifyGetDetails function to get detailed information about the change that occurred:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
procedure TForm1.WMShellNotify(var Message: TMessage); var NotifyDetails: PShChangeNotifyDetails; begin NotifyDetails := PShChangeNotifyDetails(Message.LParam); case NotifyDetails.Notification.EventId of SHCNE_CREATE: ShowMessage(‘A file or folder was created.’); SHCNE_DELETE: ShowMessage(‘A file or folder was deleted.’); SHCNE_UPDATEDIR: ShowMessage(‘A folder was modified.’); SHCNE_UPDATEITEM: ShowMessage(‘A file was modified.’); end; end; |
Make sure you check for errors and handle them appropriately when working with the Shell API.
In order to learn the file name that was created or modified when a shell change event occurs, you will need to use the SHChangeNotifyGetDetails function to get detailed information about the change that occurred. The SHChangeNotifyGetDetails function takes two parameters: a pointer to a SHChangeNotifyEntry structure that contains information about the change, and a flag indicating whether to return the path of the item or the path of the parent folder.
Here is an example of how to use the SHChangeNotifyGetDetails function to get the file name of a created or modified file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
procedure TForm1.WMShellNotify(var Message: TMessage); var NotifyDetails: PShChangeNotifyDetails; FileName: array[0..MAX_PATH] of Char; begin NotifyDetails := PShChangeNotifyDetails(Message.LParam); case NotifyDetails.Notification.EventId of SHCNE_CREATE, SHCNE_UPDATEITEM: begin SHChangeNotifyGetDetails(NotifyDetails, SHCNF_PATH, @FileName, MAX_PATH); ShowMessage(FileName); end; end; end; |
In this example, the WMShellNotify event handler is called when a shell change notification is received. It gets the pointer to a SHChangeNotifyDetails structure from the LParam field of the TMessage parameter. Then it checks the EventId field of the SHChangeNotifyStruct structure in the SHChangeNotifyDetails to see if the event is a file creation or modification. If the event is a file creation or modification, it calls the SHChangeNotifyGetDetails function to get the file name, passing the pointer to the SHChangeNotifyDetails structure, the flag SHCNF_PATH to indicate that the function should return the path of the item, and a pointer to a buffer that will receive the file name. The function returns the file name in the buffer and you can show the message to user.
Keep in mind that you have to make sure you deregister the shell change notification using the SHChangeNotifyDeregister function when the form is destroyed or the program is closed, otherwise it will cause memory leaks.
Leave a Reply