Skip to content

Instantly share code, notes, and snippets.

@CheetahChrome
Last active October 15, 2021 22:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CheetahChrome/5cf7b831c26f66a791f1276feb1e946a to your computer and use it in GitHub Desktop.
Save CheetahChrome/5cf7b831c26f66a791f1276feb1e946a to your computer and use it in GitHub Desktop.
Get Selected file in Explorer and Put it on the Clipboard in C#
// Add reference Com -> `Microsoft Shell Controls` and `Microsoft Internet Controls`
// and System.Windows & PresentationCore for Clipboard
foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows())
{
var filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
if (filename.ToLowerInvariant() == "explorer")
{
Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
foreach (Shell32.FolderItem item in items)
{
Clipboard.SetText(item.Path);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment