【问题标题】:Send Folder Rename Command to Windows Explorer将文件夹重命名命令发送到 Windows 资源管理器
【发布时间】:2011-12-27 17:28:40
【问题描述】:

我有一个用 .NET 制作的 shell 扩展,它创建文件夹(将其视为上下文菜单 New -> New Folder 选项克隆)并使用 InputBox 从用户那里输入文件夹的名称。相反,我想将文件夹上的重命名命令发送到 已经打开 Windows 资源管理器窗口。应该就像 Explorer 让我们命名一个新文件夹一样:

在搜索时,我发现了这个:Windows Explorer Shell Extension: create file and enter "rename" mode。它说要使用带有SVSI_EDIT 标志的IShellView::SelectItem 函数。我如何使用 .NET 做到这一点?如果这很难,还有其他方法可以做到吗?

【问题讨论】:

  • 你的shell扩展实现了IShellView COM接口吗?
  • 请注意,不推荐也不支持 .NET shell 扩展。

标签: c# .net vb.net winapi windows-explorer


【解决方案1】:

这里有一些代码可以做这种事情。你可以这样使用它:

private void button1_Click(object sender, EventArgs e)
{
    SelectItemInExplorer(Handle, @"d:\temp\new folder", 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
    {
        ILFree(folder);
        ILFree(file);
    }
}

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

[DllImport("shell32.dll")]
private static extern void ILFree(IntPtr pidl);

[DllImport("shell32.dll")]
private static extern int SHGetDesktopFolder(out IShellFolder ppshf);

[DllImport("ole32.dll")]
private static extern int CreateBindCtx(int reserved, out IBindCtx ppbc);

[ComImport, Guid("000214E6-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IShellFolder
{
    void ParseDisplayName(IntPtr hwnd, IBindCtx pbc, [In, MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName, out uint pchEaten, out IntPtr ppidl, ref uint pdwAttributes);
    // NOTE: we declared only what we needed...
}

private static IntPtr GetShellFolderChildrenRelativePIDL(IntPtr hwnd, IShellFolder parentFolder, string displayName)
{
    IBindCtx bindCtx;
    CreateBindCtx(0, out bindCtx);
    uint pchEaten;
    uint pdwAttributes = 0;
    IntPtr ppidl;
    parentFolder.ParseDisplayName(hwnd, bindCtx, displayName, out pchEaten, out ppidl, ref pdwAttributes);
    return ppidl;
}

private static IntPtr PathToAbsolutePIDL(IntPtr hwnd, string path)
{
    IShellFolder desktopFolder;
    SHGetDesktopFolder(out desktopFolder);
    return GetShellFolderChildrenRelativePIDL(hwnd, desktopFolder, path);
}

【讨论】:

    【解决方案2】:

    这是一种间接的方法,但您可以使用SendKeys 函数将 F2 键发送到当前打开的 Windows 资源管理器窗口,然后模拟键入所需文件夹名称并发送 Enter 键.

    【讨论】:

    • 如果您知道文件夹名称,请输入其名称以获得焦点,否则只需按日期排序文件夹,然后发送 {END} 键。
    • 您可以自己激活资源管理器窗口,然后再激活您的应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    相关资源
    最近更新 更多