// Allows a client to specify which individual items should be enumerated. // Note: The host calls this method for each item in the folder. Return S_OK, to have the item enumerated. // Return S_FALSE to prevent the item from being enumerated. public Int32 ShouldShow( Object psf, // A pointer to the folder's IShellFolder interface. IntPtr pidlFolder, // The folder's PIDL. IntPtr pidlItem) // The item's PIDL. { // check extension, and if not ok return 1 (S_FALSE) // get display name of item IShellFolder isf = (IShellFolder)psf; ShellApi.STRRET ptrDisplayName; isf.GetDisplayNameOf(pidlItem, (uint)ShellApi.SHGNO.SHGDN_NORMAL | (uint)ShellApi.SHGNO.SHGDN_FORPARSING, out ptrDisplayName); String sDisplay; ShellApi.StrRetToBSTR(ref ptrDisplayName, (IntPtr)0, out sDisplay); // check if item is file or folder IntPtr[] aPidl = new IntPtr[1]; aPidl[0] = pidlItem; uint Attrib; Attrib = (uint)ShellApi.SFGAO.SFGAO_FOLDER; int temp; temp = isf.GetAttributesOf(1, aPidl, ref Attrib); // if item is a folder, accept if ((Attrib & (uint)ShellApi.SFGAO.SFGAO_FOLDER) == (uint)ShellApi.SFGAO.SFGAO_FOLDER) { return(0); } // if item is file, check if it has a valid extension for (int i = 0; i < ValidExtension.Length; i++) { if (sDisplay.ToUpper().EndsWith("." + ValidExtension[i].ToUpper())) { return(0); } } return(1); }
public void ShowDialog() { m_FullName = ""; m_DisplayName = ""; // Get shell's memory allocator, it is needed to free some memory later IMalloc pMalloc; pMalloc = ShellFunctions.GetMalloc(); IntPtr pidlRoot; if (RootType == RootTypeOptions.BySpecialFolder) { ShellApi.SHGetFolderLocation(hwndOwner, (int)RootSpecialFolder, UserToken, 0, out pidlRoot); } else // m_RootType = RootTypeOptions.ByPath { uint iAttribute; ShellApi.SHParseDisplayName(RootPath, IntPtr.Zero, out pidlRoot, 0, out iAttribute); } ShellApi.BROWSEINFO bi = new ShellApi.BROWSEINFO(); bi.hwndOwner = hwndOwner; bi.pidlRoot = pidlRoot; bi.pszDisplayName = new String(' ', 256); bi.lpszTitle = Title; bi.ulFlags = (uint)DetailsFlags; bi.lParam = 0; bi.lpfn = new ShellApi.BrowseCallbackProc(this.myBrowseCallbackProc); // Show dialog IntPtr pidlSelected; pidlSelected = ShellApi.SHBrowseForFolder(ref bi); // Save the display name m_DisplayName = bi.pszDisplayName.ToString(); IShellFolder isf = ShellFunctions.GetDesktopFolder(); ShellApi.STRRET ptrDisplayName; isf.GetDisplayNameOf(pidlSelected, (uint)ShellApi.SHGNO.SHGDN_NORMAL | (uint)ShellApi.SHGNO.SHGDN_FORPARSING, out ptrDisplayName); String sDisplay; ShellApi.StrRetToBSTR(ref ptrDisplayName, pidlRoot, out sDisplay); m_FullName = sDisplay; if (pidlRoot != IntPtr.Zero) { pMalloc.Free(pidlRoot); } if (pidlSelected != IntPtr.Zero) { pMalloc.Free(pidlSelected); } Marshal.ReleaseComObject(isf); Marshal.ReleaseComObject(pMalloc); }