Related Items
Shell folders and Shell items can have related items. Technically, Shell-related items are exposed through the Windows IRelatedItem interface and all derived interfaces.
CBFS Shell supports that feature using special custom properties. These properties are not declared by Windows and are private to CBFS Shell:
- IIdentityName -> callback.ShellBoost.Core.PropertyStore.CurrentItem
- IDelegateItem -> callback.ShellBoost.Core.PropertyStore.DelegateItem
- IDisplayItem -> callback.ShellBoost.Core.PropertyStore.DisplayItem
- IIdentityName -> callback.ShellBoost.Core.PropertyStore.IdentityName
- IPreviewItem -> callback.ShellBoost.Core.PropertyStore.PreviewItem
- ITransferMediumItem -> callback.ShellBoost.Core.PropertyStore.TransferMediumItem
- IViewStateIdentityItem -> callback.ShellBoost.Core.PropertyStore.ViewStateIdentityItem
One common usage for these relations is to associate a virtual item (without a physical file) to a physical file (e.g., in on-demand/lazy loading scenarios).
The following code demonstrates a virtual item, associated
with a physical path. No file exists at the given physical path. The file is
created (or updated) only when the Shell asks for it through a related item
(here, PreviewItem). Shell asks for a related item before it falls back
to the item's content, which creates the opportunity to the application to write that
file down before the originator of the request can access the file.
public class VirtualAndPhysicalShellItem : ShellItem
{
public VirtualAndPhysicalShellItem(ShellFolder parent, string name)
: base(parent, new StringKeyShellItemId(name))
{
// this item is virtual, but we give it a filesystem path (with nothing in there)
// so it's half virtual, half physical
FileSystemPath = Path.Combine(Path.GetFullPath("Data"), VirtualAndPhysicalShellFolder.PhysicalStorageName, name + ".txt");
}
// Shell is asking for something
protected override bool TryGetFileSystemValue(PropertyKey key, out object value)
{
if (key == PropertyStore.PreviewItem)
{
// make sure the directory exists
IOUtilities.FileCreateDirectory(FileSystemPath);
// write something in the file
// we use a CBFS Shell utility to avoid sharing violation errors
IOUtilities.WrapSharingViolations(() =>
{
File.WriteAllText(FileSystemPath, "hello from " + DisplayName + " at " + DateTime.Now);
});
}
return base.TryGetFileSystemValue(key, out value);
}
}