发送文件夹重命名命令的Windows资源管理器 [英] Send Folder Rename Command to Windows Explorer
问题描述
我有一个外壳扩展在.NET中提出,创建文件夹(可以把它作为一个上下文菜单中选择New - >新建文件夹选项克隆),并使用一个输入框输入来自用户的文件夹的名称。相反,我要的文件夹发送rename命令到已经打开 Windows资源管理器窗口。应该就像浏览器是如何让我们命名一个新的文件夹:
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:
在搜索,我发现这一点:<一href="http://stackoverflow.com/questions/6258846/windows-explorer-shell-extension-create-file-and-enter-rename-mode">Windows资源管理器外壳扩展:创建文件,并Enter]键,重新命名&QUOT;模式。它说,使用与 SVSI_EDIT
标志 IShellView ::的SelectItem
功能。我如何做到这一点的。NET?如果是这样的辛苦,有另一种方式做同样的?
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?
推荐答案
下面是一些code,做这样的事情。您可以使用这样的:
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);
}
而code:
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);
}
这篇关于发送文件夹重命名命令的Windows资源管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!