Beispiel #1
0
        /// <summary>
        ///     Create a new instance of the <see cref="ShellLibrary" /> class
        ///     to the specified library name.
        /// </summary>
        /// <param name="libraryName">Name of the library to be created.</param>
        /// <param name="isReadOnly">A value that indicates whether the library is readonly.</param>
        /// <returns>Created <see cref="ShellLibrary" />.</returns>
        public static ShellLibrary Load(string libraryName, bool isReadOnly = true)
        {
            Contract.Requires <ArgumentNullException>(libraryName != null);
            Contract.Requires <ArgumentException>(!String.IsNullOrWhiteSpace(libraryName));
            Contract.Ensures(Contract.Result <ShellLibrary>() != null);

            var shellItemPath = Path.Combine(ShellKnownFolders.Libraries.Path, libraryName + FileExtension);

            IShellItem shellItem;
            var        guid = new Guid(ShellIID.IShellItem);
            var        hr   = ShellNativeMethods.SHCreateItemFromParsingName(shellItemPath, IntPtr.Zero, ref guid, out shellItem);

            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }

            var shellLibraryInterface = CreateShellLibraryNativeInterface();

            var flags = isReadOnly ? STGM.STGM_READ : STGM.STGM_READWRITE;

            shellLibraryInterface.LoadLibraryFromItem(shellItem, flags);

            return(new ShellLibrary(new ShellItem((IShellItem2)shellItem), shellLibraryInterface, libraryName));
        }
Beispiel #2
0
        /// <summary>
        ///     プロパティの初期設定を行います。
        /// </summary>
        private void Initialize()
        {
            // SearchCondition初期化
            SearchFolderItemFactory.SetCondition(this.SearchCondition.SearchConditionNative);

            // SearchScopePaths初期化
            var shellItems    = new List <IShellItem>(this.SearchScopePaths.Length);
            var shellItemGuid = new Guid(ShellIID.IShellItem);

            foreach (var path in this.SearchScopePaths)
            {
                IShellItem scopeShellItem;
                var        hr = ShellNativeMethods.SHCreateItemFromParsingName(path, IntPtr.Zero, ref shellItemGuid, out scopeShellItem);
                if (HRESULT.Succeeded(hr))
                {
                    shellItems.Add(scopeShellItem);
                }
            }

            var scopeShellItemArray = new ShellItemArray(shellItems.ToArray());
            var result = SearchFolderItemFactory.SetScope(scopeShellItemArray);

            if (HRESULT.Failed(result))
            {
                throw ShellException.FromHRESULT(result);
            }
        }
Beispiel #3
0
        public void SetFolderLogicalViewMode(FolderLogicalViewMode mode)
        {
            var hr = SearchFolderItemFactory.SetFolderLogicalViewMode((FOLDERLOGICALVIEWMODE)mode);

            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }
        }
Beispiel #4
0
        public void SetIconSize(int value)
        {
            var hr = SearchFolderItemFactory.SetIconSize(value);

            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }
        }
Beispiel #5
0
        public void SetFolderTypeId(Guid value)
        {
            Contract.Requires <ArgumentNullException>(value != null);

            var hr = SearchFolderItemFactory.SetFolderTypeID(value);

            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }
        }
Beispiel #6
0
        public void SetDisplayName(string displayName)
        {
            Contract.Requires <ArgumentException>(!String.IsNullOrEmpty(displayName));

            var hr = SearchFolderItemFactory.SetDisplayName(displayName);

            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }
        }
Beispiel #7
0
        public void SetGroupColumn(ShellPropertyKey propertyKey)
        {
            Contract.Requires <ArgumentNullException>(propertyKey != null);

            var key = propertyKey.PropertyKeyNative;
            var hr  = SearchFolderItemFactory.SetGroupColumn(ref key);

            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }
        }
Beispiel #8
0
        private static ShellItem CreateShellItemInfo()
        {
            Contract.Ensures(Contract.Result <ShellItem>() != null);

            var        guid = new Guid(ShellIID.IShellItem);
            IShellItem shellItem;
            var        hr = SearchFolderItemFactory.GetShellItem(ref guid, out shellItem);

            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }

            return(new ShellItem((IShellItem2)shellItem));
        }
        /// <summary>
        ///     Get the parent <see cref="ShellItem" />.
        /// </summary>
        /// <returns>parent <see cref="ShellItem" />. If the parent does not exist, it returns <c>null</c>.</returns>
        /// <exception cref="ShellException">Failed to acquire parent <see cref="ShellItem" />.</exception>
        public ShellItem GetParent()
        {
            IShellItem parentShellItem;
            var        hr = this.ShellItemInterface.GetParent(out parentShellItem);

            if (hr == COMErrorCodes.MK_E_NOOBJECT)
            {
                return(null);
            }
            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }

            return(new ShellItem((IShellItem2)parentShellItem));
        }
        /// <summary>
        ///     Create a new instance of the <see cref="ShellFolderItem" /> class
        ///     to the specified <see cref="ShellItem" />.
        /// </summary>
        /// <param name="shellItem"><see cref="ShellItem" />.</param>
        /// <returns></returns>
        public static ShellFolderItem FromShellItem(ShellItem shellItem)
        {
            object result;

            var handler = ShellBHID.BHID_SFObject;
            var hr      = shellItem.ShellItemInterface.BindToHandler(
                IntPtr.Zero,
                ref handler,
                ref ShellIIDGuid.IShellFolder,
                out result);

            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }

            return(new ShellFolderItem(result as IShellFolder));
        }
        /// <summary>
        ///     Get <see cref="IStream" />.
        /// </summary>
        /// <returns><see cref="IStream" />.</returns>
        /// <exception cref="IStream">Failed to acquire <see cref="IStream" />.</exception>
        internal IStream GetStream()
        {
            Contract.Ensures(Contract.Result <IStream>() != null);

            object result;

            var handler = ShellBHID.BHID_Stream;
            var hr      = this.ShellItemInterface.BindToHandler(
                IntPtr.Zero,
                ref handler,
                ref ShellIIDGuid.IStream,
                out result);

            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }

            return((IStream)result);
        }
Beispiel #12
0
        /// <summary>
        ///     Create a new instance of the <see cref="ShellKnownFolder" /> class
        ///     to the specified known folder ID.
        /// </summary>
        /// <param name="knownFolderId">Known folder GUID.</param>
        /// <returns>
        ///     <see cref="ShellKnownFolder" />
        /// </returns>
        /// <exception cref="ArgumentException">A known folder matching the specified GUID could not be created.</exception>
        public static ShellKnownFolder FromKnownFolderId(Guid knownFolderId)
        {
            Contract.Ensures(Contract.Result <ShellKnownFolder>() != null);

            IKnownFolder knownFolderNative;
            var          knownFolderManager = new KnownFolderManager();
            var          hr = knownFolderManager.GetFolder(knownFolderId, out knownFolderNative);

            if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }

            var result = CreateKnownFolder(knownFolderNative);

            if (result == null)
            {
                throw new ArgumentException(ErrorMessages.KnownFolderInvalidGuid, nameof(knownFolderId));
            }
            return(result);
        }
        /// <summary>
        ///     Get the <see cref="IEnumIDList" />.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        internal IEnumIDList EnumObjects(SHCONTF options)
        {
            IEnumIDList result;
            var         hr = this.ShellFolderInterface.EnumObjects(
                IntPtr.Zero,
                options,
                out result);

            if (hr == COMErrorCodes.Cancelled)
            {
                var inner = Marshal.GetExceptionForHR(hr);
                throw new DirectoryNotFoundException(inner.Message, inner);
            }
            else if (HRESULT.Failed(hr))
            {
                throw ShellException.FromHRESULT(hr);
            }
            // hr == S_FALSEの場合は、子が存在しない。(result = null)
            // In the case of hr == S_FALSE, there is no child.(result = null)
            return(result);
        }
        /// <summary>
        ///     Get <see cref="IShellFolder" />.
        /// </summary>
        /// <returns><see cref="IShellFolder" />.</returns>
        /// <exception cref="ShellException">Failed to acquire <see cref="IShellFolder" />.</exception>
        internal IShellFolder GetShellFolder()
        {
            Contract.Ensures(Contract.Result <IShellFolder>() != null);

            object result;

            var handler = ShellBHID.BHID_SFObject;
            var hr      = this.ShellItemInterface.BindToHandler(
                IntPtr.Zero,
                ref handler,
                ref ShellIIDGuid.IShellFolder,
                out result);

            if (HRESULT.Failed(hr))
            {
                //if (!String.Equals(this.ParsingName, Environment.GetFolderPath(Environment.SpecialFolder.Desktop), StringComparison.InvariantCultureIgnoreCase))
                //{
                //    throw ShellException.FromHRESULT(hr);
                //}
                throw ShellException.FromHRESULT(hr);
            }

            return(result as IShellFolder);
        }