/// <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) : this() { Debug.Assert(sourceKnownFolder != null); // Keep a reference locally knownFolder = sourceKnownFolder; nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); AccessModes flags = isReadOnly ? AccessModes.Read : AccessModes.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(LocalizedMessages.ShellLibraryInvalidLibrary, "sourceKnownFolder"); } catch (NotImplementedException) { throw new ArgumentException(LocalizedMessages.ShellLibraryInvalidLibrary, "sourceKnownFolder"); } }
/// <summary> /// Creates a shell library in a given local folder, /// using the given shell library name. /// </summary> /// <param name="libraryName">The name of this library</param> /// <param name="folderPath">The path to the local folder</param> /// <param name="overwrite">Override an existing library with the same name</param> public ShellLibrary(string libraryName, string folderPath, bool overwrite) : this() { if (string.IsNullOrEmpty(libraryName)) { throw new ArgumentException(LocalizedMessages.ShellLibraryEmptyName, "libraryName"); } if (!Directory.Exists(folderPath)) { throw new DirectoryNotFoundException(LocalizedMessages.ShellLibraryFolderNotFound); } Name = libraryName; ShellNativeMethods.LibrarySaveOptions flags = overwrite ? ShellNativeMethods.LibrarySaveOptions.OverrideExisting : ShellNativeMethods.LibrarySaveOptions.FailIfThere; Guid guid = new Guid(ShellIIDGuid.IShellItem); ShellNativeMethods.SHCreateItemFromParsingName(folderPath, IntPtr.Zero, ref guid, out IShellItem shellItemIn); nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); nativeShellLibrary.Save(shellItemIn, libraryName, flags, out nativeShellItem); }
/// <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(); AccessModes flags = isReadOnly ? AccessModes.Read : AccessModes.ReadWrite; nativeShellLibrary.LoadLibraryFromItem(nativeShellItem, flags); ShellLibrary library = new ShellLibrary(nativeShellLibrary); try { library.nativeShellItem = (IShellItem2)nativeShellItem; library.Name = libraryName; return(library); } catch { library.Dispose(); throw; } }
/// <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); string shellItemPath = System.IO.Path.Combine(librariesFolderPath, libraryName + FileExtension); int hr = ShellNativeMethods.SHCreateItemFromParsingName(shellItemPath, IntPtr.Zero, ref guid, out IShellItem nativeShellItem); if (!CoreErrorHelper.Succeeded(hr)) { throw new ShellException(hr); } INativeShellLibrary nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); AccessModes flags = isReadOnly ? AccessModes.Read : AccessModes.ReadWrite; nativeShellLibrary.LoadLibraryFromItem(nativeShellItem, flags); ShellLibrary library = new ShellLibrary(nativeShellLibrary); try { library.nativeShellItem = (IShellItem2)nativeShellItem; library.Name = libraryName; return(library); } catch { library.Dispose(); throw; } }
/// <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> /// Creates a shell library in a given local folder, /// using the given shell library name. /// </summary> /// <param name="libraryName">The name of this library</param> /// <param name="folderPath">The path to the local folder</param> /// <param name="overwrite">Override an existing library with the same name</param> public ShellLibrary(string libraryName, string folderPath, bool overwrite) { CoreHelpers.ThrowIfNotWin7(); if (String.IsNullOrEmpty(libraryName)) { throw new ArgumentNullException("libraryName", "libraryName cannot be empty."); } if (!Directory.Exists(folderPath)) { throw new DirectoryNotFoundException("Folder path not found."); } this.Name = libraryName; ShellNativeMethods.LIBRARYSAVEFLAGS flags = overwrite ? ShellNativeMethods.LIBRARYSAVEFLAGS.LSF_OVERRIDEEXISTING : ShellNativeMethods.LIBRARYSAVEFLAGS.LSF_FAILIFTHERE; Guid guid = new Guid(ShellIIDGuid.IShellItem); IShellItem shellItemIn = null; ShellNativeMethods.SHCreateItemFromParsingName(folderPath, IntPtr.Zero, ref guid, out shellItemIn); nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); nativeShellLibrary.Save(shellItemIn, libraryName, flags, out nativeShellItem); }
/// <summary> /// Release resources /// </summary> /// <param name="disposing">Indicates that this was called from Dispose(), rather than from the finalizer.</param> protected override void Dispose(bool disposing) { if (nativeShellLibrary != null) { Marshal.ReleaseComObject(nativeShellLibrary); nativeShellLibrary = null; } base.Dispose(disposing); }
/// <summary> /// Creates a shell library in the Libraries Known Folder, /// using the given shell library name. /// </summary> /// <param name="libraryName">The name of this library</param> /// <param name="overwrite">Allow overwriting an existing library; if one exists with the same name</param> public ShellLibrary(string libraryName, bool overwrite) : this() { if (string.IsNullOrEmpty(libraryName)) { throw new ArgumentException("Library Name Empty!", "libraryName"); } this.Name = libraryName; var guid = new Guid(InterfaceGuids.Libraries); var flags = overwrite ? LibrarySaveOptions.OverrideExisting : LibrarySaveOptions.FailIfThere; nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); nativeShellLibrary.SaveInKnownFolder(ref guid, libraryName, flags, out m_ComInterface); }
/// <summary> /// Creates a shell library in the Libraries Known Folder, /// using the given shell library name. /// </summary> /// <param name="libraryName">The name of this library</param> /// <param name="overwrite">Allow overwriting an existing library; if one exists with the same name</param> public ShellLibrary(string libraryName, bool overwrite) : this() { if (string.IsNullOrEmpty(libraryName)) { throw new ArgumentException(LocalizedMessages.ShellLibraryEmptyName, "libraryName"); } Name = libraryName; Guid guid = new Guid(ShellKFIDGuid.Libraries); ShellNativeMethods.LibrarySaveOptions flags = overwrite ? ShellNativeMethods.LibrarySaveOptions.OverrideExisting : ShellNativeMethods.LibrarySaveOptions.FailIfThere; nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); nativeShellLibrary.SaveInKnownFolder(ref guid, libraryName, flags, out nativeShellItem); }
/// <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(); AccessModes flags = isReadOnly ? AccessModes.Read : AccessModes.ReadWrite; nativeShellLibrary.LoadLibraryFromItem(nativeShellItem, flags); ShellLibrary library = new ShellLibrary(nativeShellLibrary); library.nativeShellItem = (IShellItem2)nativeShellItem; return(library); }
private static ShellLibrary Load_Helper(IShellItem nativeShellItem, string libraryName, bool isReadOnly) { INativeShellLibrary nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); var flags = isReadOnly ? AccessModes.Read : AccessModes.ReadWrite; nativeShellLibrary.LoadLibraryFromItem(nativeShellItem, flags); var library = new ShellLibrary(nativeShellLibrary); try { library.ComInterface = (IShellItem)nativeShellItem; library.Name = libraryName; return(library); } catch { library.Dispose(); throw; } }
/// <summary> /// Creates a shell library in a given Known Folder, /// using the given shell library name. /// </summary> /// <param name="libraryName">The name of this library</param> /// <param name="sourceKnownFolder">The known folder</param> /// <param name="overwrite">Override an existing library with the same name</param> public ShellLibrary(string libraryName, IKnownFolder sourceKnownFolder, bool overwrite) : this() { if (string.IsNullOrEmpty(libraryName)) { throw new ArgumentException(LocalizedMessages.ShellLibraryEmptyName, "libraryName"); } knownFolder = sourceKnownFolder; this.Name = libraryName; Guid guid = knownFolder.FolderId; LibrarySaveOptions flags = overwrite ? LibrarySaveOptions.OverrideExisting : LibrarySaveOptions.FailIfThere; nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); nativeShellLibrary.SaveInKnownFolder(ref guid, libraryName, flags, out nativeShellItem); }
/// <summary> /// Creates a shell library in the Libraries Known Folder, /// using the given shell library name. /// </summary> /// <param name="libraryName">The name of this library</param> /// <param name="overwrite">Allow overwriting an existing library; if one exists with the same name</param> public ShellLibrary(string libraryName, bool overwrite) { CoreHelpers.ThrowIfNotWin7(); if (String.IsNullOrEmpty(libraryName)) { throw new ArgumentNullException("libraryName", "libraryName cannot be empty."); } this.Name = libraryName; Guid guid = new Guid(ShellKFIDGuid.Libraries); ShellNativeMethods.LIBRARYSAVEFLAGS flags = overwrite ? ShellNativeMethods.LIBRARYSAVEFLAGS.LSF_OVERRIDEEXISTING : ShellNativeMethods.LIBRARYSAVEFLAGS.LSF_FAILIFTHERE; nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); nativeShellLibrary.SaveInKnownFolder(ref guid, libraryName, flags, out nativeShellItem); }
/// <summary> /// Creates a shell library in a given local folder, /// using the given shell library name. /// </summary> /// <param name="libraryName">The name of this library</param> /// <param name="folderPath">The path to the local folder</param> /// <param name="overwrite">Override an existing library with the same name</param> public ShellLibrary(string libraryName, string folderPath, bool overwrite) : this() { if (string.IsNullOrEmpty(libraryName)) { throw new ArgumentException("Library Name Empty!", "libraryName"); } if (!Directory.Exists(folderPath)) { throw new DirectoryNotFoundException("Library Not Found!"); } this.Name = libraryName; LibrarySaveOptions flags = overwrite ? LibrarySaveOptions.OverrideExisting : LibrarySaveOptions.FailIfThere; IShellItem shellItemIn = Shell32.SHCreateItemFromParsingName(folderPath, IntPtr.Zero, new Guid(InterfaceGuids.IShellItem)); nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); nativeShellLibrary.Save(shellItemIn, libraryName, flags, out m_ComInterface); }
//Construct the ShellLibrary object from a native Shell Library private ShellLibrary(INativeShellLibrary nativeShellLibrary) : this() { this.nativeShellLibrary = nativeShellLibrary; }
//Construct the ShellLibrary object from a native Shell Library private ShellLibrary(INativeShellLibrary nativeShellLibrary) { CoreHelpers.ThrowIfNotWin7(); this.nativeShellLibrary = nativeShellLibrary; }