Item Rename

Using Explorer, an end user can rename Shell items. This can be accomplished 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 be renamed only if the corresponding ShellItem instance has its CanRename property set to true. By default, the property is false.

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

If an item supports the renaming operation, you must override the OnOperate() method of the parent folder class to implement renaming, as follows:

protected virtual void OnOperate(ShellOperationEventArgs e)

The event argument contains the type of operation and its context (e.g., the item being renamed, the new name). The following is a sample 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. In that case, the method does not need to be overridden.

Validation

A Shell folder can validate a Shell item's name, especially during renaming operations. For example, this is how you can limit characters and name length:

protected override void OnGetNameValidCharactersEvent(object sender, GetNameValidCharactersEventArgs e)
{
    e.InvalidCharacters = "xyz";
}
 
protected override void OnGetNameMaxLengthEvent(object sender, GetNameMaxLengthEventArgs e)
{
    e.MaxLength = 30;
}

In this case, this is what the end user will see when typing an invalid character:


Copyright (c) 2022 Callback Technologies, Inc. - All rights reserved.
CBFS Shell 2022 .NET Edition - Version 22.0 [Build 8172]