示例#1
0
        /// <summary>
        ///     Create a new instance of the <see cref="ShellLibrary" /> class
        ///     to the specified <see cref="ShellItem" />.
        /// </summary>
        /// <param name="shellItem"><see cref="ShellItem" />.</param>
        /// <param name="isReadOnly">A value that indicates whether the library is readonly.</param>
        /// <returns>Created <see cref="ShellLibrary" />.</returns>
        internal static ShellLibrary FromShellItem(ShellItem shellItem, bool isReadOnly)
        {
            Contract.Requires <ArgumentNullException>(shellItem != null);
            Contract.Ensures(Contract.Result <ShellLibrary>() != null);

            var shellLibraryInterface = CreateShellLibraryNativeInterface();
            var flags = isReadOnly ? STGM.STGM_READ : STGM.STGM_READWRITE;

            shellLibraryInterface.LoadLibraryFromItem(shellItem.ShellItemInterface, flags);

            var libraryName = shellItem.GetDisplayName();

            return(new ShellLibrary(shellItem, shellLibraryInterface, libraryName));
        }
示例#2
0
        /// <summary>
        ///     Create a new instance of the <see cref="ShellLibrary" /> class
        ///     to the specified known folder.
        /// </summary>
        /// <param name="sourceKnownFolder">Shell known folder.</param>
        /// <param name="isReadOnly">A value that indicates whether the library is readonly.</param>
        /// <returns>Created <see cref="ShellLibrary" />.</returns>
        public static ShellLibrary Load(ShellKnownFolder sourceKnownFolder, bool isReadOnly = true)
        {
            Contract.Requires <ArgumentNullException>(sourceKnownFolder != null);
            Contract.Ensures(Contract.Result <ShellLibrary>() != null);

            var shellItem             = sourceKnownFolder.ShellItem.ShellItemInterface;
            var shellLibraryInterface = CreateShellLibraryNativeInterface();

            try
            {
                var guid  = sourceKnownFolder.FolderId;
                var flags = isReadOnly ? STGM.STGM_READ : STGM.STGM_READWRITE;
                shellLibraryInterface.LoadLibraryFromKnownFolder(ref guid, flags);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ErrorMessages.ShellLibraryInvalidLibrary, ex);
            }

            var libraryShellItem = new ShellItem(shellItem);
            var libraryName      = libraryShellItem.GetDisplayName();

            return(new ShellLibrary(libraryShellItem, shellLibraryInterface, libraryName));
        }