5

How can I select a file in Explorer after creating it in a ContextMenu shell extension?

I created the file using the IFileOperation API, and tried to use IShellView::SelectItem() in the IFileProgressSink::FinishOperations() callback. But the file selection is only flashed briefly before it is unselected again. I assume Explorer notices some changes to files and updates the view.

I can semi-reliably(?) await 10ms after FinishOperations and then call IShellView::SelectItem to get it working, but is there a more sensible way to select files after file operations?

Mr. Ran Dum
  • 151
  • 8
  • Have you tried selecting file after `IFileOperation::PerformOperations` returns, instead of relying on `IFileProgressSink` ? – Alex Guteniev Mar 02 '19 at 09:43
  • Yes, I tried that one too. Even if I use `IShellView::SelectItem` after `PerformOperations` Explorer's view update happens after the file selection, and therefore unselects again. – Mr. Ran Dum Mar 02 '19 at 10:49
  • Then I'm afraid there may not be a clean solution. But if the reason is that explorer posts this update message to itself by using a flavor of `PostMessage`, you could try posting you message to the same thread (I.e. STA thread from where Explorer calls your extension), after `PerformOperation` returns, so that message posted later is also handled later. Or you could try `WaitForInputIdle` on the process which loads your extension. – Alex Guteniev Mar 02 '19 at 10:56
  • Just wait the 10ms (or even 20ms if you want to live dangerously!) – Jonathan Potter Mar 02 '19 at 12:31
  • 2
    Did you try [SHCNF_FLUSH](https://learn.microsoft.com/en-us/windows/desktop/api/shlobj_core/nf-shlobj_core-shchangenotify#shcnf_flush) to ensure everybody has learned about the new file before you try to select it? – Raymond Chen Mar 04 '19 at 17:38
  • 1
    *But the file selection is only flashed briefly before it is unselected again.* - this say that really `IShellView::SelectItem` success, but after some another code deselect item or something else. i for test call `SelectItem(ILFindLastID(pidl), SVSI_SELECT )` just from `InvokeCommand` command and this is ok. folder view support delayed select. you not need wait any time. possible also set `IShellFolderViewCB` callback for folder view. inside `SFVM_FSNOTIFY::SHCNE_CREATE` can select, but still this will be delay select. you not show any your code. sure that problem in your implementation – RbMm Mar 10 '19 at 19:19

1 Answers1

0

Like Raymond Chen explained in the comment you can wait for changes using SHCNF_FLUSH.

Mr. Ran Dum
  • 151
  • 8