![](/img/trans.png)
[英]How to rename a folder in c# which is currently opened by windows explorer
[英]Send Folder Rename Command to Windows Explorer
我在.NET中創建了一個shell擴展,用於創建文件夾(將其視為上下文菜單New - > New Folder選項克隆),並使用InputBox從用戶輸入文件夾的名稱。 相反,我想將文件夾上的rename命令發送到已經打開的 Windows資源管理器窗口。 它應該像Explorer讓我們命名一個新文件夾一樣:
在搜索時,我發現了這個: Windows資源管理器Shell擴展:創建文件並進入“重命名”模式 。 它說使用帶有SVSI_EDIT
標志的IShellView::SelectItem
函數。 我如何用.NET做到這一點? 如果這很難,還有另一種方法可以做同樣的事嗎?
以下是一些執行此類操作的代碼。 你這樣使用它:
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);
}
這是一種間接方法,但您可以使用SendKeys函數將F2鍵發送到當前打開的Windows資源管理器窗口,然后模擬所需文件夾名稱的輸入並發送Enter鍵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.