12

我正在使用 SharpShell 编写一个小的新 shell 上下文菜单项,它将当前选定的文件复制到一个新的子文件夹,然后提示用户输入目录的新名称

搜索 StackOverflow,我找到了这个答案。但是,我想在 SharpShell 中做同样的事情。

我将不得不以某种方式向SVSI_EDIT它开火,我可以发现它埋在深处SharpShell.Interop,但我不确定这是如何工作的。我找不到任何文档或代码示例。

(编辑:我认为找出如何Pidl从文件名中获取 a 将是一个好的开始,但也许我根本不需要它?)

4

2 回答 2

4

您可以开始使用SharpShell创建一个项目来注册一个新的 shell 上下文菜单,就像在本教程中一样。

在这里,我们必须定义一个实现SharpContextMenu. 为简单起见,我们将为任何文件类型创建菜单并始终显示它:

[ComVisible(true)]
[COMServerAssociation(AssociationType.AllFiles)]
public class CopyFilesExtension : SharpContextMenu
{
    protected override bool CanShowMenu()
    {
        return true;
    }

    protected override ContextMenuStrip CreateMenu()
    {
        var menu = new ContextMenuStrip();

        var copyFiles = new ToolStripMenuItem { Text = "Copy Files To Folder..." };

        copyFiles.Click += (sender, args) => CopyFiles();
        menu.Items.Add(copyFiles);

        return menu;
    }

    private void CopyFiles()
    {
        ...
    }
} 

但我相信你已经完成了这一切,这里的问题是实现CopyFiles()方法。

一种方法是显示一个询问文件夹名称的对话框,如下所示:

复制文件对话框

然后,CopyFiles()像这样实现:

private void CopyFiles()
{
    using (var dialog = new CopyFileDialog())
    {
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            var folder = Path.GetDirectoryName(SelectedItemPaths.First());
            var newFolder = Path.Combine(folder, dialog.FolderName);

            Directory.CreateDirectory(newFolder);

            foreach (var path in SelectedItemPaths)
            {
                var newPath = Path.Combine(newFolder, Path.GetFileName(path));
                File.Move(path, newPath);
            }
        }
    }
}

在上面的代码中,我们询问了文件夹的名称,然后创建文件夹,最后将选定的文件移动到该文件夹​​。

但是,如果您想在Windows 资源管理器中使用重命名命令来执行此操作,我们可以从导入一些必需的Win32函数开始:

class Win32
{
    [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
    public static extern IntPtr ILCreateFromPath([In, MarshalAs(UnmanagedType.LPWStr)] string pszPath);

    [DllImport("shell32.dll")]
    public static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, IntPtr[] apidl, int dwFlags);

    [DllImport("shell32.dll")]
    public static extern void ILFree(IntPtr pidl);
}
  • ILCreateFromPath允许我们从文件名中获取PIDL
  • SHOpenFolderAndSelectItems允许我们选择文件并发送重命名命令。
  • ILFree释放非托管PIDL创建。

使用这些Win32函数,我们可以定义CopyFiles()如下:

private void CopyFiles()
{
    var folder = Path.GetDirectoryName(SelectedItemPaths.First());
    var newFolder = Path.Combine(folder, "New Folder");
    Directory.CreateDirectory(newFolder);

    foreach (var path in SelectedItemPaths)
    {
        var newPath = Path.Combine(newFolder, Path.GetFileName(path));
        File.Move(path, newPath);
    }

    RenameInExplorer(newFolder);
}


private static void RenameInExplorer(string itemPath)
{
    IntPtr folder = Win32.ILCreateFromPath(Path.GetDirectoryName(itemPath));
    IntPtr file = Win32.ILCreateFromPath(itemPath);

    try
    {
        Win32.SHOpenFolderAndSelectItems(folder, 1, new[] { file }, 1);
    }
    finally
    {
        Win32.ILFree(folder);
        Win32.ILFree(file);
    }
}

我们不能使用SharpShell.Interop.Shell32,因为此类中唯一可用的方法ShellExecuteEx()是用于启动新进程。

于 2016-03-25T16:57:24.410 回答
3

使用问题中引用示例SelectItemInExplorer中的功能,一个非常基本的实现将如下所示。在可能的情况下,任何 P/Invoke 功能都被重写以尽可能多地使用 SharpShell 的现有声明。

using SharpShell.Attributes;
using SharpShell.Interop;
using SharpShell.SharpContextMenu;
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace SendToFolderRename
{
    [ComVisible(true)]
    [COMServerAssociation(AssociationType.AllFiles)]
    public class SendToFolderRename : SharpContextMenu
    {
        [DllImport("ole32.dll")]
        private static extern int CreateBindCtx(int reserved, out IntPtr ppbc);

        [DllImport("shell32.dll")]
        private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, IntPtr[] apidl, int dwFlags);

        protected override bool CanShowMenu()
        {
            return true;
        }

        protected override ContextMenuStrip CreateMenu()
        {
            var menu = new ContextMenuStrip();
            var itemCountLines = new ToolStripMenuItem
            {
                Text = "Copy files to subfolder"
            };

            itemCountLines.Click += CopyFilesToSubfolder;
            menu.Items.Add(itemCountLines);
            return menu;
        }

        private void CopyFilesToSubfolder(object sender, EventArgs e)
        {
            //System.Diagnostics.Debugger.Break();
            string firstSelectedFile = SelectedItemPaths.FirstOrDefault();

            if (string.IsNullOrEmpty(firstSelectedFile))
                return;

            string currentDirPath = (new FileInfo(firstSelectedFile)).DirectoryName;
            string newDirName = Path.GetRandomFileName();
            string newDirPath = Path.Combine(currentDirPath, newDirName);
            DirectoryInfo newDir = Directory.CreateDirectory(newDirPath);
            foreach (string filePath in SelectedItemPaths)
            {
                FileInfo fileInfo = new FileInfo(filePath);
                string newFilePath = Path.Combine(fileInfo.DirectoryName, newDirName, fileInfo.Name);
                File.Copy(filePath, newFilePath);
            }

            SelectItemInExplorer(IntPtr.Zero, newDirPath, true);
        }

        public static void SelectItemInExplorer(IntPtr hwnd, string itemPath, bool edit)
        {
            if (itemPath == null)
                throw new ArgumentNullException("itemPath");

            IntPtr folder = PathToAbsolutePIDL(hwnd, Path.GetDirectoryName(itemPath));
            IntPtr file = PathToAbsolutePIDL(hwnd, itemPath);
            try
            {
                SHOpenFolderAndSelectItems(folder, 1, new[] { file }, edit ? 1 : 0);
            }
            finally
            {
                Shell32.ILFree(folder);
                Shell32.ILFree(file);
            }
        }

        private static IntPtr GetShellFolderChildrenRelativePIDL(IntPtr hwnd, IShellFolder parentFolder, string displayName)
        {
            IntPtr bindCtx;
            CreateBindCtx(0, out bindCtx);

            uint pchEaten = 0;
            SFGAO pdwAttributes = 0;
            IntPtr ppidl;
            parentFolder.ParseDisplayName(hwnd, bindCtx, displayName, ref pchEaten, out ppidl, ref pdwAttributes);
            return ppidl;
        }

        private static IntPtr PathToAbsolutePIDL(IntPtr hwnd, string path)
        {
            IShellFolder desktopFolder;
            Shell32.SHGetDesktopFolder(out desktopFolder);
            return GetShellFolderChildrenRelativePIDL(hwnd, desktopFolder, path);
        }
    }
}
于 2016-03-21T04:00:39.453 回答