/// <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); } this.Name = libraryName; ShellNativeMethods.LibrarySaveOptions flags = overwrite ? ShellNativeMethods.LibrarySaveOptions.OverrideExisting : ShellNativeMethods.LibrarySaveOptions.FailIfThere; Guid guid = new Guid(ShellIIDGuid.IShellItem); IShellItem shellItemIn; ShellNativeMethods.SHCreateItemFromParsingName(folderPath, IntPtr.Zero, ref guid, out shellItemIn); nativeShellLibrary = (INativeShellLibrary) new ShellLibraryCoClass(); nativeShellLibrary.Save(shellItemIn, libraryName, flags, out nativeShellItem); }
/// <summary> /// Creates a ShellObject given a parsing name /// </summary> /// <param name="parsingName"></param> /// <returns>A newly constructed ShellObject object</returns> internal static ShellObject Create(string parsingName) { if (string.IsNullOrEmpty(parsingName)) { throw new ArgumentNullException("parsingName"); } // Create a native shellitem from our path IShellItem2 nativeShellItem; Guid guid = new Guid(ShellIIDGuid.IShellItem2); int retCode = ShellNativeMethods.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref guid, out nativeShellItem); if (!CoreErrorHelper.Succeeded(retCode)) { throw new ShellException(LocalizedMessages.ShellObjectFactoryUnableToCreateItem, Marshal.GetExceptionForHR(retCode)); } return(ShellObjectFactory.Create(nativeShellItem)); }
/// <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 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; } }