internal FileInfoEx(PIDL fullPIDL, bool freeFullPIDL) { init(fullPIDL); checkProperties(); if (freeFullPIDL) fullPIDL.Free(); }
//private static Bitmap loadJumbo(string lookup, bool forceLoadFromDisk, IconSize size) //{ // SysImageList _imgList = isVistaUp() && ((size == IconSize.thumbnail) || (size == IconSize.jumbo)) ? _imgListJumbo : _imgListXL; // try // { // Icon icon = _imgList.Icon(_imgList.IconIndex(lookup, forceLoadFromDisk)); // Bitmap bitmap = icon.ToBitmap(); // icon.Dispose(); // System.Drawing.Color empty = System.Drawing.Color.FromArgb(0, 0, 0, 0); // if (bitmap.Width < 256) // bitmap = resizeImage(bitmap, new System.Drawing.Size(256, 256), 0); // else if (bitmap.GetPixel(100, 100) == empty && bitmap.GetPixel(200, 200) == empty && bitmap.GetPixel(200, 200) == empty) // { // _imgList.ImageListSize = SysImageListSize.largeIcons; // bitmap = resizeJumbo(_imgList.Icon(_imgList.IconIndex(lookup)).ToBitmap(), new System.Drawing.Size(200, 200), 5); // } // return bitmap; // } // catch // { // return GetFileIcon(lookup, IconSize.large); // } //} protected override Bitmap KeyToBitmap(string key, IconSize size) { try { bool isDiskFolder = key.EndsWith(":\\"); bool isTemp = key.Equals(tempPath); string ext = PathEx.GetExtension(key).ToLower(); if (!ext.StartsWith(".")) { if (specialExtFilter.Split(',').Contains(key)) { switch (ext) { case ".exe": ShellDll.PIDL pidlLookup = FileSystemInfoEx.FromString(key).PIDL; try { return(GetFileIcon(pidlLookup.Ptr, size)); } finally { if (pidlLookup != null) { pidlLookup.Free(); } } case ".lnk": using (ShellLink sl = new ShellLink(key)) switch (size) { case IconSize.small: return(sl.SmallIcon.ToBitmap()); default: return(sl.LargeIcon.ToBitmap()); } } } } if (fileBasedFSFilter.Split(',').Contains(key)) { return(GetFileBasedFSBitmap(key, size)); } else if (key == "" || key.StartsWith(".")) //Extension { return(GetFileIcon(key, size)); } else if (IsSpecialFolder(key)) { ShellDll.PIDL pidlLookup = FileSystemInfoEx.FromString(key).PIDL; try { return(GetFileIcon(pidlLookup.Ptr, size)); } finally { if (pidlLookup != null) { pidlLookup.Free(); } } } else { switch (size) { case IconSize.thumbnail: return(loadThumbnail(key, isDiskFolder || isTemp)); //case IconSize.jumbo: //case IconSize.extraLarge: // return loadJumbo(key, isDiskFolder || isTemp, size); //case IconSize.large : // _imgList.ImageListSize = SysImageListSize.largeIcons; // return _imgList.Icon(_imgList.IconIndex(key, isDiskFolder)).ToBitmap(); //case IconSize.small : // _imgList.ImageListSize = SysImageListSize.smallIcons; // return _imgList.Icon(_imgList.IconIndex(key, isDiskFolder)).ToBitmap(); default: try { return(GetFileIcon(key, size)); } catch { return(KeyToBitmap(UCUtils.GetProgramPath(), size)); } } } } catch { return(new Bitmap(1, 1)); } }
protected override void WndProc(ref Message m) { if (m.Msg == (int)ShellAPI.WM.SH_NOTIFY) { ShellAPI.SHNOTIFYSTRUCT shNotify = (ShellAPI.SHNOTIFYSTRUCT)Marshal.PtrToStructure(m.WParam, typeof(ShellAPI.SHNOTIFYSTRUCT)); //Console.Out.WriteLine("Event: {0}", (ShellAPI.SHCNE)m.LParam); //if (shNotify.dwItem1 != IntPtr.Zero) //PIDL.Write(shNotify.dwItem1); //if (shNotify.dwItem2 != IntPtr.Zero) //PIDL.Write(shNotify.dwItem2); try { switch ((ShellAPI.SHCNE)m.LParam) { #region File Changes case ShellAPI.SHCNE.CREATE: #region Create Item { if (!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child, relative; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); ShellItem parentItem = br.GetShellItem(parentPIDL); if (parentItem != null && parentItem.FilesExpanded && !parentItem.SubFiles.Contains(child)) { ShellAPI.SHGetRealIDL( parentItem.ShellFolder, child, out relative); parentItem.AddItem(new ShellItem(br, parentItem, relative)); } Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.RENAMEITEM: #region Rename Item { if (!PIDL.IsEmpty(shNotify.dwItem1) && !PIDL.IsEmpty(shNotify.dwItem2)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); if (item != null) { item.Update(shNotify.dwItem2, ShellItemUpdateType.Renamed); } } } #endregion break; case ShellAPI.SHCNE.DELETE: #region Delete Item { if (!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); ShellItem parentItem = br.GetShellItem(parentPIDL); if (parentItem != null && parentItem.SubFiles.Contains(child)) { parentItem.RemoveItem(parentItem.SubFiles[child]); } Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.UPDATEITEM: #region Update Item { if (!PIDL.IsEmpty(shNotify.dwItem1)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); if (item != null) { Console.Out.WriteLine("Item: {0}", item); item.Update(IntPtr.Zero, ShellItemUpdateType.Updated); item.Update(IntPtr.Zero, ShellItemUpdateType.IconChange); } } } #endregion break; #endregion #region Folder Changes case ShellAPI.SHCNE.MKDIR: case ShellAPI.SHCNE.DRIVEADD: case ShellAPI.SHCNE.DRIVEADDGUI: #region Make Directory { if (!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child, relative; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); ShellItem parentItem = br.GetShellItem(parentPIDL); if (parentItem != null && parentItem.FoldersExpanded && !parentItem.SubFolders.Contains(child)) { ShellAPI.SHGetRealIDL( parentItem.ShellFolder, child, out relative); IntPtr shellFolderPtr; if (parentItem.ShellFolder.BindToObject( relative, IntPtr.Zero, ref ShellAPI.IID_IShellFolder, out shellFolderPtr) == ShellAPI.S_OK) { parentItem.AddItem(new ShellItem(br, parentItem, relative, shellFolderPtr)); } else { Marshal.FreeCoTaskMem(relative); } } Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.RENAMEFOLDER: #region Rename Directory { if (!PIDL.IsEmpty(shNotify.dwItem1) && !PIDL.IsEmpty(shNotify.dwItem2)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, false)); if (item != null) { //Console.Out.WriteLine("Update: {0}", item); item.Update(shNotify.dwItem2, ShellItemUpdateType.Renamed); } } } #endregion break; case ShellAPI.SHCNE.RMDIR: case ShellAPI.SHCNE.DRIVEREMOVED: #region Remove Directory { if (!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); ShellItem parentItem = br.GetShellItem(parentPIDL); if (parentItem != null && parentItem.SubFolders.Contains(child)) { parentItem.RemoveItem(parentItem.SubFolders[child]); } Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.UPDATEDIR: case ShellAPI.SHCNE.ATTRIBUTES: #region Update Directory { if (!PIDL.IsEmpty(shNotify.dwItem1)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); if (item != null) { item.Update(IntPtr.Zero, ShellItemUpdateType.Updated); item.Update(IntPtr.Zero, ShellItemUpdateType.IconChange); } } } #endregion break; case ShellAPI.SHCNE.MEDIAINSERTED: case ShellAPI.SHCNE.MEDIAREMOVED: #region Media Change { if (!PIDL.IsEmpty(shNotify.dwItem1)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); if (item != null) { item.Update(IntPtr.Zero, ShellItemUpdateType.MediaChange); } } } #endregion break; #endregion #region Other Changes case ShellAPI.SHCNE.ASSOCCHANGED: #region Update Images { } #endregion break; case ShellAPI.SHCNE.NETSHARE: case ShellAPI.SHCNE.NETUNSHARE: break; case ShellAPI.SHCNE.UPDATEIMAGE: UpdateRecycleBin(); break; #endregion } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } base.WndProc(ref m); }
protected override void WndProc(ref Message m) { if (m.Msg == (int)ShellAPI.WM.SH_NOTIFY) { ShellAPI.SHNOTIFYSTRUCT shNotify = (ShellAPI.SHNOTIFYSTRUCT)Marshal.PtrToStructure(m.WParam, typeof(ShellAPI.SHNOTIFYSTRUCT)); //Console.Out.WriteLine("Event: {0}", (ShellAPI.SHCNE)m.LParam); //if (shNotify.dwItem1 != IntPtr.Zero) //PIDL.Write(shNotify.dwItem1); //if (shNotify.dwItem2 != IntPtr.Zero) //PIDL.Write(shNotify.dwItem2); switch ((ShellAPI.SHCNE)m.LParam) { #region File Changes case ShellAPI.SHCNE.CREATE: #region Create Item { if (!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child, relative; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); ShellItem parentItem = br.GetShellItem(parentPIDL); if (parentItem != null && parentItem.FilesExpanded && !parentItem.SubFiles.Contains(child)) { ShellAPI.SHGetRealIDL( parentItem.ShellFolder, child, out relative); parentItem.AddItem(new ShellItem(br, parentItem, relative)); } Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.RENAMEITEM: #region Rename Item { if (!PIDL.IsEmpty(shNotify.dwItem1) && !PIDL.IsEmpty(shNotify.dwItem2)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); if (item != null) item.Update(shNotify.dwItem2, ShellItemUpdateType.Renamed); } } #endregion break; case ShellAPI.SHCNE.DELETE: #region Delete Item { if (!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); ShellItem parentItem = br.GetShellItem(parentPIDL); if (parentItem != null && parentItem.SubFiles.Contains(child)) parentItem.RemoveItem(parentItem.SubFiles[child]); Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.UPDATEITEM: #region Update Item { if (!PIDL.IsEmpty(shNotify.dwItem1)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); if (item != null) { Console.Out.WriteLine("Item: {0}", item); item.Update(IntPtr.Zero, ShellItemUpdateType.Updated); item.Update(IntPtr.Zero, ShellItemUpdateType.IconChange); } } } #endregion break; #endregion #region Folder Changes case ShellAPI.SHCNE.MKDIR: case ShellAPI.SHCNE.DRIVEADD: case ShellAPI.SHCNE.DRIVEADDGUI: #region Make Directory { if (!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child, relative; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); ShellItem parentItem = br.GetShellItem(parentPIDL); if (parentItem != null && parentItem.FoldersExpanded && !parentItem.SubFolders.Contains(child)) { ShellAPI.SHGetRealIDL( parentItem.ShellFolder, child, out relative); IntPtr shellFolderPtr; if (parentItem.ShellFolder.BindToObject( relative, IntPtr.Zero, ref ShellAPI.IID_IShellFolder, out shellFolderPtr) == ShellAPI.S_OK) { parentItem.AddItem(new ShellItem(br, parentItem, relative, shellFolderPtr)); } else Marshal.FreeCoTaskMem(relative); } Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.RENAMEFOLDER: #region Rename Directory { if (!PIDL.IsEmpty(shNotify.dwItem1) && !PIDL.IsEmpty(shNotify.dwItem2)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, false)); if (item != null) { //Console.Out.WriteLine("Update: {0}", item); item.Update(shNotify.dwItem2, ShellItemUpdateType.Renamed); } } } #endregion break; case ShellAPI.SHCNE.RMDIR: case ShellAPI.SHCNE.DRIVEREMOVED: #region Remove Directory { if (!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); ShellItem parentItem = br.GetShellItem(parentPIDL); if (parentItem != null && parentItem.SubFolders.Contains(child)) parentItem.RemoveItem(parentItem.SubFolders[child]); Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.UPDATEDIR: case ShellAPI.SHCNE.ATTRIBUTES: #region Update Directory { if (!PIDL.IsEmpty(shNotify.dwItem1)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); if (item != null) { item.Update(IntPtr.Zero, ShellItemUpdateType.Updated); item.Update(IntPtr.Zero, ShellItemUpdateType.IconChange); } } } #endregion break; case ShellAPI.SHCNE.MEDIAINSERTED: case ShellAPI.SHCNE.MEDIAREMOVED: #region Media Change { if (!PIDL.IsEmpty(shNotify.dwItem1)) { ShellItem item = br.GetShellItem(new PIDL(shNotify.dwItem1, true)); if (item != null) item.Update(IntPtr.Zero, ShellItemUpdateType.MediaChange); } } #endregion break; #endregion #region Other Changes case ShellAPI.SHCNE.ASSOCCHANGED: #region Update Images { } #endregion break; case ShellAPI.SHCNE.NETSHARE: case ShellAPI.SHCNE.NETUNSHARE: break; case ShellAPI.SHCNE.UPDATEIMAGE: UpdateRecycleBin(); break; #endregion } } base.WndProc(ref m); }
/// <summary> /// Rename a file or folder in an directory. /// </summary> public static void Rename(string source, string destName) { //DirectoryInfoEx srcDir = new DirectoryInfoEx(Path.GetDirectoryName(source)); FileSystemInfoEx srcElement = new FileSystemInfoEx(source); string srcName = Path.GetFileName(source); if (!srcElement.Exists || srcElement.Parent == null) throw new IOException("Source not exists"); //0.15: Fixed ShellFolder not freed. using (ShellFolder2 srcParentShellFolder = srcElement.Parent.ShellFolder) { if (srcParentShellFolder == null) throw new IOException("Source directory does not support IShellFolder"); IntPtr tmpPtr; int hr = srcParentShellFolder.SetNameOf(IntPtr.Zero, srcElement.PIDLRel.Ptr, destName, ShellAPI.SHGNO.FORPARSING, out tmpPtr); PIDL tmpPIDL = new PIDL(tmpPtr, false); //consume the IntPtr, and free it. tmpPIDL.Free(); if (hr != ShellAPI.S_OK) Marshal.ThrowExceptionForHR(hr); } }
protected override void WndProc(ref Message m) { if(m.Msg == (int)ShellAPI.WM.SH_NOTIFY) { ShellAPI.SHNOTIFYSTRUCT shNotify = (ShellAPI.SHNOTIFYSTRUCT)Marshal.PtrToStructure(m.WParam, typeof(ShellAPI.SHNOTIFYSTRUCT)); //Console.Out.WriteLine("Event: {0}", (ShellAPI.SHCNE)m.LParam); //if (shNotify.dwItem1 != IntPtr.Zero) //PIDL.Write(shNotify.dwItem1); //if (shNotify.dwItem2 != IntPtr.Zero) //PIDL.Write(shNotify.dwItem2); switch((ShellAPI.SHCNE)m.LParam) { #region File Changes case ShellAPI.SHCNE.CREATE: #region Create Item { if(!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.RENAMEITEM: break; case ShellAPI.SHCNE.DELETE: #region Delete Item { if(!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.UPDATEITEM: break; #endregion #region Folder Changes case ShellAPI.SHCNE.MKDIR: case ShellAPI.SHCNE.DRIVEADD: case ShellAPI.SHCNE.DRIVEADDGUI: #region Make Directory { if(!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.RENAMEFOLDER: break; case ShellAPI.SHCNE.RMDIR: case ShellAPI.SHCNE.DRIVEREMOVED: #region Remove Directory { if(!PIDL.IsEmpty(shNotify.dwItem1)) { IntPtr parent, child; PIDL.SplitPidl(shNotify.dwItem1, out parent, out child); PIDL parentPIDL = new PIDL(parent, false); Marshal.FreeCoTaskMem(child); parentPIDL.Free(); } } #endregion break; case ShellAPI.SHCNE.UPDATEDIR: case ShellAPI.SHCNE.ATTRIBUTES: break; case ShellAPI.SHCNE.MEDIAINSERTED: case ShellAPI.SHCNE.MEDIAREMOVED: break; #endregion #region Other Changes case ShellAPI.SHCNE.ASSOCCHANGED: #region Update Images { } #endregion break; case ShellAPI.SHCNE.NETSHARE: case ShellAPI.SHCNE.NETUNSHARE: break; case ShellAPI.SHCNE.UPDATEIMAGE: UpdateRecycleBin(); break; #endregion } } base.WndProc(ref m); }