IEngineGetMenuCommandAsync Method |
Namespace: ITHit.FileSystem
Task<IMenuCommand> GetMenuCommandAsync( Guid menuGuid, IOperationContext operationContext = null )
To show context menu in Windows Explorer for your virtual drive follow these steps:
The code below is part of 'VirtualDrive' C# sample provided with the SDK.
public class MenuCommandLock : IMenuCommandWindows { private readonly VirtualEngineBase engine; private readonly ILogger logger; private const string lockCommandIcon = @"Images\Locked.ico"; private const string unlockCommandIcon = @"Images\Unlocked.ico"; public MenuCommandLock(VirtualEngineBase engine, ILogger logger) { this.engine = engine; this.logger = logger.CreateLogger("Lock Menu Command"); } public async Task<string> GetTitleAsync(IEnumerable<string> filesPath) { bool isLocked = await IsLockedAsync(filesPath) == true; return isLocked ? "Unlock" : "Lock"; } public async Task<string> GetIconAsync(IEnumerable<string> filesPath) { string iconName = await IsLockedAsync(filesPath) == false ? lockCommandIcon : unlockCommandIcon; string iconPath = Path.Combine(Path.GetDirectoryName(typeof(MenuCommandLock).Assembly.Location), iconName); return iconPath; } public async Task<MenuState> GetStateAsync(IEnumerable<string> filesPath) { // This sample can not lock folders. // Hide menu if any folders are selected. foreach (string userFileSystemPath in filesPath) { FileAttributes attr = File.GetAttributes(userFileSystemPath); if ((attr & FileAttributes.Directory) == FileAttributes.Directory) return MenuState.Hidden; } bool? isLocked = await IsLockedAsync(filesPath); return isLocked.HasValue ? MenuState.Enabled : MenuState.Hidden; } public async Task InvokeAsync(IEnumerable<string> filesPath, IEnumerable<byte[]> remoteStorageItemIds = null, CancellationToken cancellationToken = default) { // If you need a remote storage ID for each item use the following code: //foreach (string userFileSystemPath in filesPath) //{ // if(engine.Placeholders.TryGetItem(userFileSystemPath, out PlaceholderItem placeholder)) // { // byte[] remoteStorageId = placeholder.GetRemoteStorageItemId(); // } //} bool isLocked = await IsLockedAsync(filesPath) == true; foreach (string userFileSystemPath in filesPath) { try { IClientNotifications clientNotifications = engine.ClientNotifications(userFileSystemPath, logger); if (isLocked) await clientNotifications.UnlockAsync(); else await clientNotifications.LockAsync(); } catch (Exception ex) { string actionName = isLocked ? "Unlock" : "Lock"; logger.LogError($"Failed to {actionName} item", userFileSystemPath, null, ex); } } } public async Task<string> GetToolTipAsync(IEnumerable<string> filesPath) { bool isLocked = await IsLockedAsync(filesPath) == true; return isLocked ? "Unlock item(s)" : "Lock item(s)"; } private async Task<bool?> IsLockedAsync(IEnumerable<string> filesPath, CancellationToken cancellationToken = default) { bool? allLocked = null; foreach (string userFileSystemPath in filesPath) { try { bool isLocked = false; if (engine.Placeholders.TryGetItem(userFileSystemPath, out PlaceholderItem placeholder)) { if (placeholder.Properties.TryGetActiveLockInfo(out ServerLockInfo lockInfo)) { // Detect if locked by this user. if (!engine.IsCurrentUser(lockInfo.Owner)) { // Typically we can not unlock items locked by other users. // We must hide or disable menu in this case. return null; } isLocked = true; } } if (allLocked.HasValue && (allLocked != isLocked)) { return null; } allLocked = isLocked; } catch (Exception ex) { logger.LogError("Failed to get lock state", userFileSystemPath, null, ex); } } return allLocked; } }