Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tshego3/f8ddc4e5ccb801cd100fc79a7c302f0e to your computer and use it in GitHub Desktop.
Save tshego3/f8ddc4e5ccb801cd100fc79a7c302f0e 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