コード例 #1
0
        /// <summary>
        /// Gets the selected <see cref="Attributes"/> for this item
        /// </summary>
        /// <param name="mask">The attributes required</param>
        /// <returns>The results of the item's attributes and the <paramref name="mask"/> attributes</returns>
        public Attributes GetAttributes(Attributes mask)
        {
            SFGAO attr;

            _pItem.GetAttributes((SFGAO)mask, out attr);
            return((Attributes)attr);
        }
コード例 #2
0
        /// <summary>
        /// Creates a ShellObject given a native IShellItem interface
        /// </summary>
        /// <param name="nativeShellItem"></param>
        /// <returns>A newly constructed ShellObject object</returns>
        internal static ShellObject Create(IShellItem nativeShellItem)
        {
            // Sanity check
            Debug.Assert(nativeShellItem != null, "nativeShellItem should not be null");

            // Need to make sure we're running on Vista or higher
            if (!(Environment.OSVersion.Version.Major >= 6))
            {
                throw new PlatformNotSupportedException("ShellObjectFactoryPlatformNotSupported");
            }

            // A lot of APIs need IShellItem2, so just keep a copy of it here
            IShellItem2 nativeShellItem2 = nativeShellItem as IShellItem2;

            // Get the System.ItemType property
            string itemType = ShellHelper.GetItemType(nativeShellItem2);

            if (!string.IsNullOrEmpty(itemType))
            {
                itemType = itemType.ToUpperInvariant();
            }

            // Get some IShellItem attributes
            ShellNativeMethods.ShellFileGetAttributesOptions sfgao;
            nativeShellItem2.GetAttributes(ShellNativeMethods.ShellFileGetAttributesOptions.FileSystem | ShellNativeMethods.ShellFileGetAttributesOptions.Folder, out sfgao);

            // Is this item a FileSystem item?
            bool isFileSystem = (sfgao & ShellNativeMethods.ShellFileGetAttributesOptions.FileSystem) != 0;

            // Is this item a Folder?
            bool isFolder = (sfgao & ShellNativeMethods.ShellFileGetAttributesOptions.Folder) != 0;

            // Create the right type of ShellObject based on the above information

            // 1. First check if this is a Shell Link
            if (itemType == ".lnk")
            {
                throw new Exception("No links allowed");
            }
            // 2. Check if this is a container or a single item (entity)
            else if (isFolder)
            {
                throw new Exception("No folders");
            }

            // 6. If this is an entity (single item), check if its filesystem or not
            if (isFileSystem)
            {
                return(new ShellFile(nativeShellItem2));
            }

            throw new Exception("No other things");
        }
コード例 #3
0
        /// <summary>
        /// Creates a ShellObject given a native IShellItem interface
        /// </summary>
        /// <param name="nativeShellItem"></param>
        /// <returns>A newly constructed ShellObject object</returns>
        internal static ShellObject Create(IShellItem nativeShellItem)
        {
            // Sanity check
            Debug.Assert(nativeShellItem != null, "nativeShellItem should not be null");

            // Need to make sure we're running on Vista or higher
            if (!CoreHelpers.RunningOnVista)
            {
                throw new PlatformNotSupportedException(LocalizedMessages.ShellObjectFactoryPlatformNotSupported);
            }

            // A lot of APIs need IShellItem2, so just keep a copy of it here
            IShellItem2 nativeShellItem2 = nativeShellItem as IShellItem2;

            // Get the System.ItemType property
            string itemType = ShellHelper.GetItemType(nativeShellItem2);

            if (!string.IsNullOrEmpty(itemType))
            {
                itemType = itemType.ToUpperInvariant();
            }

            // Get some IShellItem attributes
            ShellNativeMethods.ShellFileGetAttributesOptions sfgao;
            nativeShellItem2.GetAttributes(ShellNativeMethods.ShellFileGetAttributesOptions.FileSystem | ShellNativeMethods.ShellFileGetAttributesOptions.Folder, out sfgao);

            // Is this item a FileSystem item?
            bool isFileSystem = (sfgao & ShellNativeMethods.ShellFileGetAttributesOptions.FileSystem) != 0;

            // Is this item a Folder?
            bool isFolder = (sfgao & ShellNativeMethods.ShellFileGetAttributesOptions.Folder) != 0;

            // Shell Library
            ShellLibrary shellLibrary = null;

            // Create the right type of ShellObject based on the above information

            // 1. First check if this is a Shell Link
            if (itemType == ".lnk")
            {
                return(new ShellLink(nativeShellItem2));
            }

            // 2. Check if this is a container or a single item (entity)
            else if (isFolder)
            {
                // 3. If this is a folder, check for types: Shell Library, Shell Folder or Search Container
                if (itemType == ".library-ms" && (shellLibrary = ShellLibrary.FromShellItem(nativeShellItem2, true)) != null)
                {
                    return(shellLibrary); // we already created this above while checking for Library
                }
                else if (itemType == ".searchconnector-ms")
                {
                    return(new ShellSearchConnector(nativeShellItem2));
                }
                else if (itemType == ".search-ms")
                {
                    return(new ShellSavedSearchCollection(nativeShellItem2));
                }

                // 4. It's a ShellFolder
                if (isFileSystem)
                {
                    // 5. Is it a (File-System / Non-Virtual) Known Folder
                    if (!IsVirtualKnownFolder(nativeShellItem2))
                    { // needs to check if it is a known folder and not virtual
                        FileSystemKnownFolder kf = new FileSystemKnownFolder(nativeShellItem2);
                        return(kf);
                    }

                    return(new ShellFileSystemFolder(nativeShellItem2));
                }

                // 5. Is it a (Non File-System / Virtual) Known Folder
                if (IsVirtualKnownFolder(nativeShellItem2))
                { // needs to check if known folder is virtual
                    NonFileSystemKnownFolder kf = new NonFileSystemKnownFolder(nativeShellItem2);
                    return(kf);
                }

                return(new ShellNonFileSystemFolder(nativeShellItem2));
            }

            // 6. If this is an entity (single item), check if its filesystem or not
            if (isFileSystem)
            {
                return(new ShellFile(nativeShellItem2));
            }

            return(new ShellNonFileSystemItem(nativeShellItem2));
        }
コード例 #4
0
        internal static ShellObject Create(IShellItem nativeShellItem)
        {
            // Sanity check
            Debug.Assert(nativeShellItem != null, "nativeShellItem should not be null");

            // Need to make sure we're running on Vista or higher
            if (!CoreHelpers.RunningOnVista)
            {
                throw new PlatformNotSupportedException("Shell Object creation requires Windows Vista or higher operating system.");
            }

            // A lot of APIs need IShellItem2, so just keep a copy of it here
            IShellItem2 nativeShellItem2 = nativeShellItem as IShellItem2;

            // Get the System.ItemType property
            string itemType = ShellHelper.GetItemType(nativeShellItem2);

            if (!string.IsNullOrEmpty(itemType))
            {
                itemType = itemType.ToLower();
            }

            // Get some IShellItem attributes
            ShellNativeMethods.SFGAO sfgao;
            nativeShellItem2.GetAttributes(ShellNativeMethods.SFGAO.SFGAO_FILESYSTEM | ShellNativeMethods.SFGAO.SFGAO_FOLDER, out sfgao);

            // Is this item a FileSystem item?
            bool isFileSystem = (sfgao & ShellNativeMethods.SFGAO.SFGAO_FILESYSTEM) != 0;

            // Is this item a Folder?
            bool isFolder = (sfgao & ShellNativeMethods.SFGAO.SFGAO_FOLDER) != 0;

            // Shell Library
            ShellLibrary shellLibrary = null;

            // For KnownFolders
            bool isKnownFolderVirtual = false;

            // Create the right type of ShellObject based on the above information

            // 1. First check if this is a Shell Link
            if (itemType == ".lnk")
            {
                return(new ShellLink(nativeShellItem2));
            }
            // 2. Check if this is a container or a single item (entity)
            else if (isFolder)
            {
                // 3. If this is a folder, check for types: Shell Library, Shell Folder or Search Container
                if ((itemType == ".library-ms") && (shellLibrary = ShellLibrary.FromShellItem(nativeShellItem2, true)) != null)
                {
                    return(shellLibrary); // we already created this above while checking for Library
                }
                else if (itemType == ".searchconnector-ms")
                {
                    return(new ShellSearchConnector(nativeShellItem2));
                }
                else if ((itemType == ".search-ms"))
                {
                    return(new ShellSavedSearchCollection(nativeShellItem2));
                }
                else
                {
                    // 4. It's a ShellFolder
                    if (isFileSystem)
                    {
                        // 5. Is it a (File-System / Non-Virtual) Known Folder
                        if ((GetNativeKnownFolder(nativeShellItem2, out isKnownFolderVirtual) != null) && !isKnownFolderVirtual)
                        {
                            FileSystemKnownFolder kf = new FileSystemKnownFolder(nativeShellItem2);
                            return(kf);
                        }
                        else
                        {
                            return(new ShellFileSystemFolder(nativeShellItem2));
                        }
                    }
                    else
                    {
                        // 5. Is it a (Non File-System / Virtual) Known Folder
                        if ((GetNativeKnownFolder(nativeShellItem2, out isKnownFolderVirtual) != null) && isKnownFolderVirtual)
                        {
                            NonFileSystemKnownFolder kf = new NonFileSystemKnownFolder(nativeShellItem2);
                            return(kf);
                        }
                        else
                        {
                            return(new ShellNonFileSystemFolder(nativeShellItem2));
                        }
                    }
                }
            }
            else
            {
                // 6. If this is an entity (single item), check if its filesystem or not
                if (isFileSystem)
                {
                    return(new ShellFile(nativeShellItem2));
                }
                else
                {
                    return(new ShellNonFileSystemItem(nativeShellItem2));
                }
            }
        }