在SharpShell上下文菜单中选择要重命名的文件

时间:2015-07-16 21:59:25

标签: c# windows-shell sharpshell

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

搜索StackOverflow,我找到this回答。但是,我想在SharpShell中做同样的事情。

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

(编辑:我认为如何从文件名中获取task :name, [:arg, :arg] => :dependency do ... end 将是一个良好的开端,但也许我根本不需要它?)

2 个答案:

答案 0 :(得分:4)

您可以使用 SharpShell 开始创建项目,以注册新的shell上下文菜单,如此tutorial

在这里,我们必须定义一个实现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()方法。

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

CopyFilesDialog

然后,像这样实施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 < / em>功能:

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(),用于启动新进程。

答案 1 :(得分:3)

使用问题中cited exampleSelectItemInExplorer功能,非常基本实现如下所示。尽可能重写任何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);
        }
    }
}