在Windows中,无论是桌面还是Windows资源管理器中,我想要检测文件或文件夹被选中(高亮)的时刻。当发生这种情况时,我希望显示一个消息框,显示文件或文件夹的完整名称。
如果选择了多个项目,我想要显示所有这些项目。
请注意,我的解决方案必须用C#编写。
在Windows中,无论是桌面还是Windows资源管理器中,我想要检测文件或文件夹被选中(高亮)的时刻。当发生这种情况时,我希望显示一个消息框,显示文件或文件夹的完整名称。
如果选择了多个项目,我想要显示所有这些项目。
请注意,我的解决方案必须用C#编写。
public void GetListOfSelectedFilesAndFolderOfWindowsExplorer()
{
string filename;
ArrayList selected = new ArrayList();
var shell = new Shell32.Shell();
//For each explorer
foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows())
{
filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
if (filename.ToLowerInvariant() == "explorer")
{
Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
foreach (Shell32.FolderItem item in items)
{
MessageBox.Show(item.Path.ToString());
selected.Add(item.Path);
}
}
}
}
在Renier的回答中补充一些内容:
SHDocVw.ShellWindows()
而不是 SHDocVw.ShellWindowsClass()
,那么您就不必调整“嵌入互操作类型”设置。请参见 https://dev59.com/gFHTa4cB1Zd3GeqPVN3p#4174056。 - peterflynn