Click here to Skip to main content
13,630,895 members
Click here to Skip to main content
Add your own
alternative version


61 bookmarked
Posted 16 Oct 2002
Licenced Ms-PL

Shell Notifications in C#

, 16 Oct 2002
Rate this:
Please Sign up or sign in to vote.
Shows how to receive shell notifications for specified directories

Shell Notifications


When you build an application that shows files or directories of the local computer, you must know if the files already exist. You can add refresh functions but there is another way to know in real time when something has changed by using the SHChangeNotifyRegister API.

For a complete overview of shell change notifications, James Holderness made a very useful "Shell Notifications" page.

The structures and enumerations are not listed here, but are accessible in the sources.

SHChangeNotifyRegister and SHChangeNotifyUnregister declarations

The API calls are hidden: SHChangeNotifyRegister uses an entry point value of 2 and SHChangeNotifyUnregister uses 4.

using System.Runtime.InteropServices;

[DllImport("shell32.dll", EntryPoint="#2", CharSet=CharSet.Auto)]
private static extern uint SHChangeNotifyRegister(
    IntPtr hWnd,
    SHCNF fSources,
    SHCNE fEvents,
    uint wMsg,
    int cEntries,
    ref SHChangeNotifyEntry pFsne);

[DllImport("shell32.dll", EntryPoint="#4", CharSet=CharSet.Auto)]
private static extern Boolean SHChangeNotifyUnregister(
    ulong hNotify);

Register a Handle

When a modification occurs, the shell sends a notification to each registered handle. You have to specify the folder root by the pidl retrieved by the SHGetSpecialFolderLocation API.

[DllImport("shell32.dll", CharSet=CharSet.Auto)]
private static extern uint SHGetSpecialFolderLocation(
    IntPtr hWnd,
    CSIDL nFolder,
    out IntPtr Pidl);

public ulong RegisterChangeNotify(IntPtr hWnd, CSIDL FolderID,
    bool Recursively)
    if(notifyid != 0) return(0);
    SHChangeNotifyEntry changeentry = new SHChangeNotifyEntry();

    changeentry.pIdl = GetPidlFromFolderID(hWnd, FolderID);
    changeentry.Recursively = Recursively;

    notifyid = SHChangeNotifyRegister(
        ref changeentry);


public Boolean UnregisterChangeNotify()
    if(notifyid == 0) return(false);

        notifyid = 0;


public static IntPtr GetPidlFromFolderID(IntPtr hWnd, CSIDL Id)
    IntPtr pIdl = IntPtr.Zero;

    SHGetFolderLocationReturnValues res = 
		SHGetSpecialFolderLocation( hWnd,
            out pIdl);


Notification Reception

When you receive a notification, you want to know the type of the operation, the concerned item, and the new value of the item. This information is stored in the SHNOTIFYSTRUCT which the WParam points to. You can then add the new notification to an ArrayList that contains each notification.

public System.Collections.ArrayList NotificationsReceived = 
    new System.Collections.ArrayList();

public bool NotificationReceipt(IntPtr wParam, IntPtr lParam)
    SHNOTIFYSTRUCT shNotify = (SHNOTIFYSTRUCT) Marshal.PtrToStructure(

    NotifyInfos info = new NotifyInfos((SHCNE)(int) lParam);
    // These notifications are Not supported
    if(info.Notification == SHCNE.SHCNE_FREESPACE ||
        info.Notification == SHCNE.SHCNE_UPDATEIMAGE)
    info.Item1 = GetPathFromPidl(shNotify.dwItem1);
    info.Item2 = GetPathFromPidl(shNotify.dwItem2);
    // Was this notification in the received notifications ?



If the notification was not in the NotificationsReceived list, the NotificationReceipt method adds it to the list and returns true.

Now, a new case must be added in the WndProc of the Form, so that the notifications can be handled:

The code below handles the notification by checking if it has been added to the list.  if it isn't, NotificationReceipt returns true and the code calls NewOperation passing in the NotifyInfos struct corresponding to the latest event.

private ShellNotifications Notifications = new ShellNotifications();

protected override void WndProc(ref Message m)
        case (int) ShellNotifications.WM_SHNOTIFY:
            bool bAdded = Notifications.NotificationReceipt(
                m.WParam, m.LParam);

            if( bAdded )
                NotifyInfos nis = (NotifyInfos) 
                        Notifications.NotificationsReceived.Count - 1];


    base.WndProc(ref m);

private void NewOperation(NotifyInfos infos)
    // You can add your code here.
    // The line below is an example : 
    // it adds the operation informations in a listview.
        new ListViewItem(
            new string[] {

Revision History

  • October 17, 2002 - Initial post


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Thomas Caudal
Web Developer
France France
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionSHChangeNotifyRegister Does not notifies when USB is ejected in Non Admin Mode Pin
harsha_techi16-Mar-15 4:39
memberharsha_techi16-Mar-15 4:39 
GeneralMy vote of 5 Pin
malac113-Jul-10 9:26
membermalac113-Jul-10 9:26 
QuestionCompact framework compatibility?? Pin
nuttynibbles20-Feb-10 7:29
membernuttynibbles20-Feb-10 7:29 
GeneralSHChangeNotifyUnregister Pin
bzucko5-Mar-07 2:52
memberbzucko5-Mar-07 2:52 
Generalthis only works from explorer Pin
Billiebub12-Feb-07 1:16
memberBilliebub12-Feb-07 1:16 
GeneralGetDisplayNameFromPidl Pin
Lesuran9-Nov-04 3:44
memberLesuran9-Nov-04 3:44 
GeneralRe: GetDisplayNameFromPidl Pin
felix at home24-Sep-06 23:07
memberfelix at home24-Sep-06 23:07 
GeneralGet path name directly instead pidls Pin
Freigeist21-Sep-04 6:34
memberFreigeist21-Sep-04 6:34 
Questioncan not log all changes Pin
zbshen19-May-04 7:43
memberzbshen19-May-04 7:43 
AnswerRe: can not log all changes Pin
SimmRein10-Sep-09 16:58
memberSimmRein10-Sep-09 16:58 
QuestionHow to watch for viewing & executing actions Pin
zuken212-Apr-04 13:54
memberzuken212-Apr-04 13:54 
GeneralCan't download although i logged in - that really bugs me! Pin
AnonymousAnNoyedAndRageous25-Jan-03 16:26
sussAnonymousAnNoyedAndRageous25-Jan-03 16:26 
GeneralFileSystemWatcher has platform issues Pin
James Cadd17-Oct-02 7:41
memberJames Cadd17-Oct-02 7:41 
GeneralRe: FileSystemWatcher has platform issues Pin
Jesper17-Oct-02 22:29
memberJesper17-Oct-02 22:29 
GeneralRe: FileSystemWatcher has platform issues Pin
James Cadd18-Oct-02 1:46
memberJames Cadd18-Oct-02 1:46 
GeneralRe: FileSystemWatcher has platform issues Pin
Jesper18-Oct-02 1:56
memberJesper18-Oct-02 1:56 
GeneralRe: FileSystemWatcher has platform issues Pin
malac113-Jul-10 9:27
membermalac113-Jul-10 9:27 
GeneralSystem.IO.FileSystemWatcher Pin
Jesper17-Oct-02 2:51
memberJesper17-Oct-02 2:51 
GeneralRe: System.IO.FileSystemWatcher Pin
Paul Watson17-Oct-02 3:34
editorPaul Watson17-Oct-02 3:34 
GeneralRe: System.IO.FileSystemWatcher Pin
Daniel Turini17-Oct-02 3:40
memberDaniel Turini17-Oct-02 3:40 
GeneralRe: System.IO.FileSystemWatcher Pin
Paul Watson17-Oct-02 3:46
editorPaul Watson17-Oct-02 3:46 
GeneralRe: System.IO.FileSystemWatcher Pin
Daniel Turini17-Oct-02 3:56
memberDaniel Turini17-Oct-02 3:56 
GeneralRe: System.IO.FileSystemWatcher Pin
Jesper17-Oct-02 4:05
memberJesper17-Oct-02 4:05 
GeneralRe: System.IO.FileSystemWatcher Pin
Paul Watson17-Oct-02 4:28
editorPaul Watson17-Oct-02 4:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180712.1 | Last Updated 17 Oct 2002
Article Copyright 2002 by Thomas Caudal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid