Item rename support

Using Windows Explorer, the end user can rename Shell Items. It can be done using context menus (if the ‘Rename’ menu item is visible), or by pressing the F2 key while the item is selected. In all cases, the item can only be renamed if the corresponding ShellItem instance has its CanRename property set to true. By default, it’s not.

Renaming is done item by item. A folder can perfectly contain items (including sub folders) that can be renamed and others that cannot.

If a ShellItem supports renaming, to handle the rename operation of a ShellItem instance, you must override the OnOperate() method of the parent folder class:

protected virtual void OnOperate(ShellOperationEventArgs e)

The event argument contains the type of operation and its context (the item being renamed, the new name, etc.). So for example, here is an implementation of OnOperate:

protected override void OnOperate(ShellOperationEventArgs e)
{
    switch (e.Operation)
    {
        case ShellOperation.SetNameOf:
        case ShellOperation.RenameItem:
            OnRename(e);
            break;
    }
}
 
private void OnRename(ShellOperationEventArgs e)
{
    if (e.Item.IsFolder)
    {
        // TODO: rename a folder
        return;
    }
 
    // TODO: rename an item
}

Note 1: Explorer has multiple ways of renaming an item, so you should handle both SetNameOf and RenameItem operations.

Note 2: if the ShellItem instance represents a physical file item, the default implementation of OnOperate() will rename the physical file, so the method does not need to be overridden in that case.