public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { try { if (value is FileSystemInfoEx) { PIDL pidlLookup = (value as FileSystemInfoEx).PIDL; try { Icon ico = GetFileIcon(pidlLookup.Ptr); return(ico != null?loadBitmap(ico.ToBitmap()) : null); } finally { if (pidlLookup != null) { pidlLookup.Free(); } pidlLookup = null; } } } catch { } return(null); }
/// <summary>Gets the icon for the item using the specified characteristics.</summary> /// <param name="psf">The IShellFolder from which to request the IExtractIcon instance.</param> /// <param name="pidl">The PIDL of the item within <paramref name="psf"/>.</param> /// <param name="imgSz">The width, in pixels, of the icon.</param> /// <param name="hico">The resulting icon handle, on success, or <c>null</c> on failure.</param> /// <returns>The result of function.</returns> public static HRESULT LoadIconFromExtractIcon(IShellFolder psf, PIDL pidl, ref uint imgSz, out SafeHICON hico) { hico = default; HRESULT hr = psf.GetUIObjectOf((IntPtr)pidl, out IExtractIconW ieiw); if (hr.Succeeded) { try { return(LoadIconFromExtractIcon(ieiw, ref imgSz, out hico)); } finally { Marshal.ReleaseComObject(ieiw); } } else if ((hr = psf.GetUIObjectOf((IntPtr)pidl, out IExtractIconA iei)).Succeeded) { try { return(LoadIconFromExtractIcon(iei, ref imgSz, out hico)); } finally { Marshal.ReleaseComObject(iei); } } return(hr); }
//internal static string getParentParseName(PIDL pidl) //{ // PIDL relPIDL; // PIDL parentPIDL = getParentPIDL(pidl, out relPIDL); // IShellFolder sf = getParentIShellFolder(parentPIDL, out relPIDL); // if (relPIDL.Size == 0) // return IOTools.IID_Desktop; // return loadName(sf, relPIDL, ShellAPI.SHGNO.FORPARSING); //} internal static ShellFolder2 getParentIShellFolder(PIDL pidl, out PIDL relPIDL) { int hr; IntPtr ptrShellFolder = IntPtr.Zero; if (pidl.Size == 0 || PIDL.ILFindLastID(pidl.Ptr) == pidl.Ptr || //is root or parent is root PIDLToPath(pidl) == Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) { hr = ShellAPI.SHGetDesktopFolder(out ptrShellFolder); relPIDL = new PIDL(pidl, true); } else { PIDL parentPIDL = getParentPIDL(pidl, out relPIDL); //Console.WriteLine("ParentPIDL.Size = {0}", parentPIDL.Size); Guid guid = ShellAPI.IID_IShellFolder2; using (ShellFolder2 _desktopShellFolder = getDesktopShellFolder()) hr = _desktopShellFolder.BindToObject(parentPIDL.Ptr, IntPtr.Zero, ref guid, out ptrShellFolder); if (parentPIDL != null) { parentPIDL.Free(); } } if (hr == ShellAPI.S_OK && ptrShellFolder != IntPtr.Zero) { return(new ShellFolder2(ptrShellFolder)); } Marshal.ThrowExceptionForHR(hr); return(null); //mute error. }
protected override void refresh(IShellFolder2 parentShellFolder, PIDL relPIDL, PIDL fullPIDL) { base.refresh(parentShellFolder, relPIDL, fullPIDL); if (parentShellFolder != null && relPIDL != null && fullPIDL != null) { ShellAPI.SFGAO attribute = shGetFileAttribute(fullPIDL, ShellAPI.SFGAO.BROWSABLE | ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.HASSUBFOLDER); IsBrowsable = (attribute & ShellAPI.SFGAO.BROWSABLE) != 0 || (attribute & ShellAPI.SFGAO.CONTENTSMASK) != 0; IsFileSystem = (attribute & ShellAPI.SFGAO.FILESYSTEM) != 0; HasSubFolder = (attribute & ShellAPI.SFGAO.HASSUBFOLDER) != 0; if (!FullName.StartsWith("::") && Directory.Exists(FullName)) { try { DirectoryInfo di = new DirectoryInfo(FullName); Attributes = di.Attributes; LastAccessTime = di.LastAccessTime; LastWriteTime = di.LastWriteTime; CreationTime = di.CreationTime; } catch { } } initDirectoryType(); } }
void handleEvent(WatcherChangeTypesEx changeType, bool isFolder, PIDL pidl) { if (IOTools.MatchFileMask(pidl, Filter)) { FileSystemEventArgsEx args = new FileSystemEventArgsEx(changeType, isFolder, pidl, FileSystemInfoEx.PIDLToPath(pidl)); switch (changeType) { case WatcherChangeTypesEx.Created: if (OnCreated != null) { OnCreated(this, args); } break; case WatcherChangeTypesEx.Changed: if (OnChanged != null) { OnChanged(this, args); } break; case WatcherChangeTypesEx.Deleted: if (OnDeleted != null) { OnDeleted(this, args); } break; } } }
/// <summary>Inserts the specified <see cref="PIDL"/> before the existing list.</summary> /// <param name="insertPidl">The <see cref="PIDL"/> to insert.</param> public void Insert(PIDL insertPidl) { IntPtr newPidl = IntILCombine(insertPidl.handle, handle); Free(handle); SetHandle(newPidl); }
public FolderSelectedEventArgs(PIDL pidl, bool hotspot, int location, object selectedPIDL) { SelectedPIDL = selectedPIDL; PIDL = pidl; Hotspot = hotspot; Location = location; }
internal FileInfoEx(DirectoryInfoEx parentDir, PIDL relPIDL) { Parent = parentDir; //0.15: Fixed ShellFolder not freed. using (ShellFolder2 parentShellFolder = parentDir.ShellFolder) init(parentShellFolder, parentDir.PIDLRel, relPIDL); }
protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Right) { ListViewItem item = GetItemAt(e.X, e.Y); if (item != null) { item.Selected = true; ShellContextMenu contextMenu = new ShellContextMenu(); using (PIDL pidl = ((PIDL)((PIDL)item.Tag).Clone())) { string path = pidl.PhysicalPath; if (path.EndsWith("\\")) { path = path.Substring(0, path.Length - 1); } FileInfo[] fileinfo = { new FileInfo(pidl.PhysicalPath) }; contextMenu.ShowContextMenu(Handle, fileinfo, Cursor.Position); } } } base.OnMouseDown(e); }
protected override void OnPaint(PaintEventArgs e) { int count = 0; Items = new Items(); if (data != null) { PIDL pidl = (PIDL)data.Clone(); while (!pidl.IsDesktop) { Items.Add(new Item(pidl)); pidl = (PIDL)pidl.GetParentFolder().Clone(); count++; } pidl.DisplayName = "My Computer"; Item item = Items.Add(new Item(pidl)); item.Rectangle = new Rectangle(0, 3, 16, 16); item.X = 0; item.Y = 3; item.Hotspot = new Rectangle(17, 3, 17, 16); pidl.Dispose(); int index = sysimagelist.IconIndex(data.Pidl); e.Graphics.DrawImage(sysimagelist.Icon(index).ToBitmap(), new Rectangle(3, 3, 16, 16)); DrawItems(e.Graphics); } Rectangle rect = ClientRectangle; rect.Width--; rect.Height--; e.Graphics.DrawRectangle(RectPen, rect); base.OnPaint(e); }
/// <summary>Initializes a new instance of the <see cref="ShellItem"/> class.</summary> /// <param name="parent">The parent Shell item.</param> /// <param name="pidl">The ID List of the child.</param> public ShellItem(ShellItem parent, PIDL pidl) { if (parent == null) { throw new ArgumentNullException(nameof(parent)); } if (pidl == null || pidl.IsInvalid) { throw new ArgumentNullException(nameof(pidl)); } if (!parent.IsFolder) { throw new ArgumentException("Parent argument must be a folder."); } object ppv; if (IsMinVista) { SHCreateItemWithParent(PIDL.Null, parent.GetIShellFolder(), pidl, typeof(IShellItem).GUID, out ppv); } else { var idList = PIDL.Combine(parent.PIDL, pidl); ppv = new ShellItemImpl(idList, false); } Init((IShellItem)ppv); }
public void ComEnumeratorTest3() { // Test class values var pidl = KNOWNFOLDERID.FOLDERID_Documents.PIDL(); SHGetDesktopFolder(out IShellFolder dt); var docs = (IShellFolder)dt.BindToObject(pidl, null, typeof(IShellFolder).GUID); var eo = docs.EnumObjects(IntPtr.Zero, SHCONTF.SHCONTF_FOLDERS); // Test IEnumerable var e = new IEnumFromNext <IntPtr>((out IntPtr p) => eo.Next(1, out p, out var f).Succeeded&& f == 1, () => eo.Reset()); foreach (var p in e) { Assert.That(p, Is.Not.EqualTo(IntPtr.Zero)); var sp = new PIDL(p); TestContext.WriteLine(sp); } // Test IEnumerator var g = e.GetEnumerator(); g.Reset(); Assert.That(g.MoveNext(), Is.True); g.Dispose(); Assert.That(g.MoveNext(), Is.False); }
TreeNode SelectTreeNode(TreeNode treenode, PIDL pidl) { if (treenode == null) { return(null); } if (treenode.Tag == null) { return(null); } if (pidl != null) { if (pidl.Equals(treenode.Tag)) { return(treenode); } } if (pidl != null) { if (pidl.DisplayName == ((PIDL)treenode.Tag).DisplayName && pidl.PhysicalPath == ((PIDL)treenode.Tag).PhysicalPath) { return(treenode); } } foreach (TreeNode node in treenode.Nodes) { TreeNode found = SelectTreeNode(node, pidl); if (found != null) { return(found); } } return(null); }
//0.12: Fixed PIDL, PIDLRel, ShellFolder, Storage properties generated on demand to avoid x-thread issues. internal PIDL getRelPIDL() { if (_pidlRel != null) { //0.14 : FileSystemInfoEx now stored a copy of PIDL/Rel, will return copy of it when properties is called (to avoid AccessViolation). return(new PIDL(_pidlRel, true)); } //0.16: Fixed getRelPIDL() cannot return correct value if File/DirInfoEx construct with string. (attemp to return a freed up pointer). PIDL pidlLookup = getPIDL(); try { return(getRelativePIDL(pidlLookup)); } finally { if (pidlLookup != null) { pidlLookup.Free(); } pidlLookup = null; } }
protected override void refresh(IShellFolder2 parentShellFolder, PIDL relPIDL, PIDL fullPIDL, RefreshModeEnum mode) { base.refresh(parentShellFolder, relPIDL, fullPIDL, mode); if ((mode & RefreshModeEnum.FullProps) != 0 && parentShellFolder != null && relPIDL != null && fullPIDL != null) { if (!FullName.StartsWith("::") && File.Exists(FullName)) { try { FileInfo fi = new FileInfo(FullName); IsReadOnly = fi.IsReadOnly; Attributes = fi.Attributes; Length = fi.Length; LastAccessTime = fi.LastAccessTime; LastWriteTime = fi.LastWriteTime; CreationTime = fi.CreationTime; } catch { } } else //0.18: Uses File to return FileInfo by default { ShellAPI.SFGAO attribute = shGetFileAttribute(fullPIDL, ShellAPI.SFGAO.READONLY); IsReadOnly = (attribute & ShellAPI.SFGAO.READONLY) != 0; Length = 0; } } }
public override bool Equals(object obj) { PIDL pidl = obj as PIDL; if (pidl == null) { return(false); } if (desktopFolder.CompareIDs(NativeMethods.SHCIDS.SHCIDS_CANONICALONLY, this.pidl, pidl.pidl) == 0) { return(true); } if (desktopFolder.CompareIDs(NativeMethods.SHCIDS.SHCIDS_ALLFIELDS, this.pidl, pidl.pidl) == 0) { return(true); } if (desktopFolder.CompareIDs(NativeMethods.SHCIDS.SHCIDS_BITMASK, this.pidl, pidl.pidl) == 0) { return(true); } if (desktopFolder.CompareIDs(NativeMethods.SHCIDS.SHCIDS_COLUMNMASK, this.pidl, pidl.pidl) == 0) { return(true); } return(false); }
private static FileAttributes loadAttributes(IShellFolder2 iShellFolder, PIDL pidlFull, PIDL pidlRel) { FileAttributes retVal = new FileAttributes(); //ShellAPI.SFGAO attribute = shGetFileAttribute(pidlFull, ShellAPI.SFGAO.READONLY | // ShellAPI.SFGAO.FOLDER | ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.STREAM | ShellAPI.SFGAO.FILESYSANCESTOR | // ShellAPI.SFGAO.HIDDEN); ShellAPI.SFGAO attribute = ShellAPI.SFGAO.READONLY | ShellAPI.SFGAO.FOLDER | ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.STREAM | ShellAPI.SFGAO.FILESYSANCESTOR; iShellFolder.GetAttributesOf(1, new IntPtr[] { pidlRel.Ptr }, ref attribute); if (!IOTools.IsZip(attribute) && (attribute & ShellAPI.SFGAO.FOLDER) != 0) { retVal |= FileAttributes.Directory; } if ((attribute & ShellAPI.SFGAO.HIDDEN) != 0) { retVal |= FileAttributes.Hidden; } if ((attribute & ShellAPI.SFGAO.READONLY) != 0) { retVal |= FileAttributes.ReadOnly; } return(retVal); }
public void GetItemIDTest() { IntPtr pidl = new IntPtr(); uint iAttribute; byte[] actual; string path = @"F:\Downloaded"; API.SHParseDisplayName(path, IntPtr.Zero, out pidl, 0, out iAttribute); PIDL target = new PIDL(pidl, false); // TODO: Initialize to an appropriate value actual = target.GetItemID(0); Console.Write(actual.Length.ToString() + " "); for (int i = 0; i < actual.Length; i++) { Console.Write(actual[i].ToString() + " "); } Console.WriteLine(); // next path = @"C:\Documents and Settings\Administrator\Desktop"; API.SHParseDisplayName(path, IntPtr.Zero, out pidl, 0, out iAttribute); target = new PIDL(pidl, false); // TODO: Initialize to an appropriate value actual = target.GetItemID(0); //如何判断byte[] 和null? Assert.Equals(actual == null, true); }
/// <summary> /// Refresh the file / directory info. Does not refresh directory contents /// because it refresh every time GetFiles/Directories/FileSystemInfos is called. /// </summary> public void Refresh(RefreshModeEnum mode = RefreshModeEnum.AllProps) { RefreshMode |= mode; //0.23 : Delay loading some properties. PIDL relPIDL = null; if (!Exists) { refresh(null, null, null, mode); } else { try { //0.16: Fixed ShellFolder not freed this.RequestPIDL(pidlLookup => { using (ShellFolder2 sf = getParentIShellFolder(pidlLookup, out relPIDL)) refresh(sf, relPIDL, pidlLookup, mode); }); } catch (NullReferenceException) { refresh(null, null, null, mode); } }
/// <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); } } }
private static string loadName(IShellFolder2 iShellFolder, PIDL relPidl, ShellAPI.SHGNO uFlags) { if (iShellFolder == null) { return(null); } IntPtr ptrStr = Marshal.AllocCoTaskMem(ShellAPI.MAX_PATH * 2 + 4); Marshal.WriteInt32(ptrStr, 0, 0); StringBuilder buf = new StringBuilder(ShellAPI.MAX_PATH); try { if (iShellFolder.GetDisplayNameOf(relPidl.Ptr, uFlags, ptrStr) == ShellAPI.S_OK) { ShellAPI.StrRetToBuf(ptrStr, relPidl.Ptr, buf, ShellAPI.MAX_PATH); } } finally { if (ptrStr != IntPtr.Zero) { Marshal.FreeCoTaskMem(ptrStr); } ptrStr = IntPtr.Zero; } return(buf.ToString()); }
/// <summary> /// Return whether PIDL match fileMask ( * and ? supported) /// </summary> /// <param name="pidl"></param> /// <param name="fileMask"></param> /// <returns></returns> public static bool MatchFileMask(PIDL pidl, string fileMask) { string path = FileSystemInfoEx.PIDLToPath(pidl); string name = PathEx.GetFileName(path); return(MatchFileMask(name, fileMask)); }
public static MemoryStream CreateShellIdList(string[] paths) { byte[][] pidls = new byte[paths.Length][]; for (int i = 0; i < paths.Length; i++) { PIDL pidl = ILCreateFromPath(paths[i]); pidls[i] = pidl.GetBytes(); } int pidlOffset = 4 * (paths.Length + 2); MemoryStream memoryStream = new MemoryStream(); BinaryWriter binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(paths.Length); binaryWriter.Write(pidlOffset); pidlOffset += 4; foreach (byte[] pidl in pidls) { binaryWriter.Write(pidlOffset); pidlOffset += pidl.Length; } binaryWriter.Write(0); foreach (byte[] pidl in pidls) { binaryWriter.Write(pidl); } return(memoryStream); }
public static ImageSource GetIcon(string path, SHIL size) { IntPtr result = IntPtr.Zero; SHFILEINFO shFileInfo = new SHFILEINFO(); if (path.StartsWith(".") || path.StartsWith(@"\\")) { result = SHGetFileInfo(path, FileAttributes.Normal, ref shFileInfo, Marshal.SizeOf(shFileInfo), SHGFI.SHGFI_SYSICONINDEX | SHGFI.SHGFI_USEFILEATTRIBUTES); } else { PIDL pidl = ILCreateFromPath(path); result = SHGetFileInfo(pidl, FileAttributes.Normal, ref shFileInfo, Marshal.SizeOf(shFileInfo), SHGFI.SHGFI_SYSICONINDEX | SHGFI.SHGFI_PIDL); } if (result == IntPtr.Zero) { return(null); } SHGetImageList(size, ImageListId, out IImageList iImageList); SafeHICON hIcon = iImageList.GetIcon(shFileInfo.iIcon, IMAGELISTDRAWFLAGS.ILD_IMAGE); ImageSource imageSource = hIcon.ToBitmapSource(); imageSource.Freeze(); DestroyIcon(hIcon); return(imageSource); }
private void ShellNamespaceTreeControlTestForm_Load(object sender, EventArgs e) { using var qaPidl = new PIDL("shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}"); shellNamespaceTreeControl1.RootItems.Add(new ShellFolder(qaPidl), false, true); shellNamespaceTreeControl1.RootItems.Add(ShellFolder.Desktop, true, true); propertyGrid1.SelectedObject = shellNamespaceTreeControl1; }
public void LoadFiles(PIDL pidl, PIDL selectedPIDL) { folderListBox1.IsDesktop = (pidl.DisplayName == "My Computer"); folderListBox1.SelectedPIDL = selectedPIDL; if (lastDisplayName == pidl.DisplayName) { return; } bool showHiddenObjects = false; bool showNonFolders = false; bool sortResults = true; ShellFolder folder; if (pidl.DisplayName == "My Computer") { folder = new ShellFolder(Environment.SpecialFolder.Desktop); } else { folder = new ShellFolder(pidl); } { ArrayList children = folder.GetChildren(showHiddenObjects, showNonFolders, sortResults); { folderListBox1.AddFolders(children); } } folder.Dispose(); lastDisplayName = pidl.DisplayName; }
private static string GetNameForPidl(PIDL pidl) { SafeCoTaskMemHandle mStr; try { SHGetNameFromIDList(pidl, SIGDN.SIGDN_DESKTOPABSOLUTEEDITING, out mStr); return(mStr.ToString(-1)); } catch { } try { SHGetNameFromIDList(pidl, SIGDN.SIGDN_NORMALDISPLAY, out mStr); return(mStr.ToString(-1)); } catch { } return(string.Empty); }
public void ShellFolderTest3() { using var pidl = new PIDL(testFld); Assert.That(() => { Assert.That(new ShellFolder(pidl).FileSystemPath, Is.EqualTo(testFld)); }, Throws.Nothing); Assert.That(() => new ShellFolder((PIDL)null), Throws.Exception); Assert.That(() => new ShellFolder(new PIDL(@"C:\Tamp")), Throws.Exception); Assert.That(() => new ShellFolder(new PIDL(testFile)), Throws.Nothing); }
internal FileInfoEx(DirectoryInfoEx parentDir, PIDL relPIDL) { Parent = parentDir; //0.15: Fixed ShellFolder not freed. using (ShellFolder2 parentShellFolder = parentDir.ShellFolder) parentDir.RequestRelativePIDL(parentRelPidl => init(parentShellFolder, parentRelPidl, relPIDL)); }
internal static string PIDLToPath(PIDL pidlFull) { if (DirectoryInfoEx.DesktopDirectory.RequestPIDL(desktopPIDL => pidlFull.Equals(desktopPIDL))) { return("::{00021400-0000-0000-C000-000000000046}"); } using (ShellFolder2 _desktopShellFolder = getDesktopShellFolder()) return(loadName(_desktopShellFolder, pidlFull, ShellAPI.SHGNO.FORPARSING)); }