Send Folder Rename Command to Windows Explorer

元气小坏坏 提交于 2020-01-11 06:15:10

问题


I have a shell extension made in .NET that creates folders (think of it as a context menu New -> New Folder option clone) and uses a InputBox to input the name of the folder from the user. Instead I want to send the rename command on the folder to the already open Windows Explorer window. It should be just like how Explorer lets us name a new folder:

On searching, I found this : Windows Explorer Shell Extension: create file and enter "rename" mode. It says to use the IShellView::SelectItem function with the SVSI_EDIT flag. How do I do that with .NET? If that's hard, is there another way to do the same?


回答1:


Here is some code that does this kind of things. You use it like this:

private void button1_Click(object sender, EventArgs e)
{
    SelectItemInExplorer(Handle, @"d:\temp\new folder", true);
}

And the code:

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:


This is kind of an indirect approach, but you can use the SendKeys function in order to send the F2 key to the currently open windows explorer window, and then simulate the typing of the desired folder name and send the Enter key.



来源:https://stackoverflow.com/questions/8647447/send-folder-rename-command-to-windows-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!