/// <summary> /// Creates a shell library in the Libraries Known Folder, /// using the given IKnownFolder /// </summary> /// <param name="sourceKnownFolder">KnownFolder from which to create the new Shell Library</param> /// <param name="isReadOnly">If <B>true</B> , opens the library in read-only mode.</param> private ShellLibrary(IKnownFolder sourceKnownFolder, bool isReadOnly) { CoreHelpers.ThrowIfNotWin7(); Debug.Assert(sourceKnownFolder != null); // Keep a reference locally knownFolder = sourceKnownFolder; nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); ShellNativeMethods.STGM flags = isReadOnly ? ShellNativeMethods.STGM.Read : ShellNativeMethods.STGM.ReadWrite; // Get the IShellItem2 base.nativeShellItem = ((ShellObject)sourceKnownFolder).NativeShellItem2; Guid guid = sourceKnownFolder.FolderId; // Load the library from the IShellItem2 try { nativeShellLibrary.LoadLibraryFromKnownFolder(ref guid, flags); } catch (InvalidCastException) { throw new ArgumentException("The given known folder is not a valid library.", "sourceKnownFolder"); } catch (NotImplementedException) { throw new ArgumentException("The given known folder is not a valid library.", "sourceKnownFolder"); } }
/// <summary> /// Load the library using a number of options /// </summary> /// <param name="libraryName">The name of the library.</param> /// <param name="folderPath">The path to the library.</param> /// <param name="isReadOnly">If <B>true</B>, opens the library in read-only mode.</param> /// <returns>A ShellLibrary Object</returns> public static ShellLibrary Load( string libraryName, string folderPath, bool isReadOnly) { CoreHelpers.ThrowIfNotWin7(); // Create the shell item path string shellItemPath = System.IO.Path.Combine(folderPath, libraryName + FileExtension); ShellFile item = ShellFile.FromFilePath(shellItemPath); IShellItem nativeShellItem = item.NativeShellItem; INativeShellLibrary nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); ShellNativeMethods.STGM flags = isReadOnly ? ShellNativeMethods.STGM.Read : ShellNativeMethods.STGM.ReadWrite; nativeShellLibrary.LoadLibraryFromItem(nativeShellItem, flags); ShellLibrary library = new ShellLibrary(nativeShellLibrary); library.nativeShellItem = (IShellItem2)nativeShellItem; library.Name = libraryName; return(library); }
/// <summary> /// Load the library using a number of options /// </summary> /// <param name="libraryName">The name of the library</param> /// <param name="isReadOnly">If <B>true</B>, loads the library in read-only mode.</param> /// <returns>A ShellLibrary Object</returns> public static ShellLibrary Load( string libraryName, bool isReadOnly) { CoreHelpers.ThrowIfNotWin7(); IKnownFolder kf = KnownFolders.Libraries; string librariesFolderPath = (kf != null) ? kf.Path : string.Empty; Guid guid = new Guid(ShellIIDGuid.IShellItem); IShellItem nativeShellItem; string shellItemPath = System.IO.Path.Combine(librariesFolderPath, libraryName + FileExtension); int hr = ShellNativeMethods.SHCreateItemFromParsingName(shellItemPath, IntPtr.Zero, ref guid, out nativeShellItem); if (!CoreErrorHelper.Succeeded(hr)) { throw Marshal.GetExceptionForHR(hr); } INativeShellLibrary nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); ShellNativeMethods.STGM flags = isReadOnly ? ShellNativeMethods.STGM.Read : ShellNativeMethods.STGM.ReadWrite; nativeShellLibrary.LoadLibraryFromItem(nativeShellItem, flags); ShellLibrary library = new ShellLibrary(nativeShellLibrary); library.nativeShellItem = (IShellItem2)nativeShellItem; library.Name = libraryName; return(library); }
/// <summary> /// Load the library using a number of options /// </summary> /// <param name="nativeShellItem">IShellItem</param> /// <param name="isReadOnly">read-only flag</param> /// <returns>A ShellLibrary Object</returns> internal static ShellLibrary FromShellItem( IShellItem nativeShellItem, bool isReadOnly) { CoreHelpers.ThrowIfNotWin7(); INativeShellLibrary nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); ShellNativeMethods.STGM flags = isReadOnly ? ShellNativeMethods.STGM.Read : ShellNativeMethods.STGM.ReadWrite; nativeShellLibrary.LoadLibraryFromItem(nativeShellItem, flags); ShellLibrary library = new ShellLibrary(nativeShellLibrary); library.nativeShellItem = (IShellItem2)nativeShellItem; return(library); }