コード例 #1
0
        /// <summary>
        ///     Create new shell library.
        /// </summary>
        /// <param name="libraryName">Name of the library to be created.</param>
        /// <param name="sourcePath">Library path.</param>
        /// <param name="overwrite">A value indicating whether to overwrite an existing library.</param>
        /// <returns>Created <see cref="ShellLibrary" />.</returns>
        public static ShellLibrary Create(string libraryName, string sourcePath, bool overwrite)
        {
            Contract.Requires <ArgumentException>(!String.IsNullOrWhiteSpace(libraryName));
            Contract.Requires <ArgumentException>(!String.IsNullOrWhiteSpace(sourcePath));
            Contract.Ensures(Contract.Result <ShellLibrary>() != null);

            if (!Directory.Exists(sourcePath))
            {
                throw new DirectoryNotFoundException(ErrorMessages.ShellLibraryFolderNotFound);
            }

            var        guid  = new Guid(ShellIID.IShellItem);
            var        flags = GetLibrarySaveOptions(overwrite);
            IShellItem shellItemIn;

            ShellNativeMethods.SHCreateItemFromParsingName(sourcePath, IntPtr.Zero, ref guid, out shellItemIn);

            var shellLibraryInterface = CreateShellLibraryNativeInterface();

            IShellItem2 shellItem2;

            shellLibraryInterface.Save(shellItemIn, libraryName, flags, out shellItem2);

            return(new ShellLibrary(new ShellItem(shellItem2), shellLibraryInterface, libraryName));
        }
コード例 #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);
            }
        }
コード例 #3
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));
        }
コード例 #4
0
        /// <summary>
        /// Adds a location (folder, library, search connector, known folder) to the list of
        /// places available for the user to open or save items. This method actually adds an item
        /// to the <b>Favorite Links</b> or <b>Places</b> section of the Open/Save dialog. Overload method
        /// takes in a string for the path.
        /// </summary>
        /// <param name="path">The item to add to the places list.</param>
        /// <param name="location">One of the enumeration values that indicates placement of the item in the list.</param>
        public void AddPlace(string path, FileDialogAddPlaceLocation location)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            // Get our native dialog
            if (nativeDialog == null)
            {
                InitializeNativeFileDialog();
                nativeDialog = GetNativeFileDialog();
            }

            // Create a native shellitem from our path
            IShellItem2 nativeShellItem;
            Guid        guid    = new Guid(ShellIIDGuid.IShellItem2);
            int         retCode = ShellNativeMethods.SHCreateItemFromParsingName(path, IntPtr.Zero, ref guid, out nativeShellItem);

            if (!CoreErrorHelper.Succeeded(retCode))
            {
                throw new Exception(LocalizedMessages.CommonFileDialogCannotCreateShellItem, Marshal.GetExceptionForHR(retCode));
            }

            // Add the shellitem to the places list
            if (nativeDialog != null)
            {
                nativeDialog.AddPlace(nativeShellItem, (ShellNativeMethods.FileDialogAddPlacement)location);
            }
        }
コード例 #5
0
        // T7E: Gets property store for shortcut
        public void SetApplicationIdForShortcut(string shortcutPath, string appId)
        {
            // Get shell item
            IShellItem2 shellItem;
            Guid        guid = new Guid(ShellIIDGuid.IShellItem2);
            int         hrc  = ShellNativeMethods.SHCreateItemFromParsingName(shortcutPath, IntPtr.Zero, ref guid, out shellItem);

            // Get property store
            Guid           psGuid    = new Guid(ShellIIDGuid.IPropertyStore);
            IPropertyStore propStore = null;
            int            hr        = shellItem.GetPropertyStore(
                ShellNativeMethods.GETPROPERTYSTOREFLAGS.GPS_READWRITE,
                ref psGuid,
                out propStore);

            // Set appid property
            PropVariant pv       = new PropVariant();
            PropertyKey AppIdKey = new PropertyKey(new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3"), 5);

            pv.SetString(appId);
            propStore.SetValue(ref AppIdKey, ref pv);
            propStore.Commit();

            Marshal.ReleaseComObject(propStore);
            pv.Clear();
        }
コード例 #6
0
        public static IShellItem2 ParseShellItem2Name(String value)
        {
            Guid ishellItem2GuidCopy = _ishellItem2Guid;

            IShellItem2 shellItem;
            HResult     result = ShellNativeMethods.SHCreateItemFromParsingName(value, IntPtr.Zero, ref ishellItem2GuidCopy, out shellItem);

            if (result == HResult.Ok)
            {
                return(shellItem);
            }
            else
            {
                // TODO: Ideally we'd interrogate each Win32 error to decide if this function should return null (e.g. nonsensical filename) or throw (some system error).
                // But that takes too much effort, so just always return null for now.
                return(null);
            }
        }
コード例 #7
0
        /// <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("7E9FB0D3-919F-4307-AB2E-9B1860310C93");
            int         retCode = ShellNativeMethods.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref guid, out nativeShellItem);

            if (retCode != 0)
            {
                throw new Exception("ShellObjectFactoryUnableToCreateItem", Marshal.GetExceptionForHR(retCode));
            }
            return(ShellObjectFactory.Create(nativeShellItem));
        }
コード例 #8
0
        /// <summary>
        ///     Create new instance of the <see cref="ShellItem" /> class
        ///     to the specified <c>ParsingName</c>.
        /// </summary>
        /// <param name="parsingName">Parsing Name.</param>
        /// <returns><see cref="ShellItem" />.</returns>
        /// <exception cref="ArgumentException"><paramref name="parsingName" /> is <c>null</c> or empty string.</exception>
        /// <exception cref="ShellException">Failed to create <see cref="ShellItem" />.</exception>
        public static ShellItem FromParsingName(string parsingName)
        {
            Contract.Requires <ArgumentException>(!String.IsNullOrWhiteSpace(parsingName));
            Contract.Ensures(Contract.Result <ShellItem>() != null);

            IShellItem2 shellItem2;
            var         code = ShellNativeMethods.SHCreateItemFromParsingName(
                parsingName,
                IntPtr.Zero,
                ref ShellIIDGuid.IShellItem2,
                out shellItem2);

            if (shellItem2 == null || HRESULT.Failed(code))
            {
                throw new ShellException(ErrorMessages.ShellFactoryUnableToCreateItem, Marshal.GetExceptionForHR(code));
            }
            return(new ShellItem(shellItem2));
        }
コード例 #9
0
        // T7E: Gets property store for shortcut
        public string GetApplicationIdForShortcut(string shortcutPath)
        {
            // Get shell item
            IShellItem2 shellItem;
            Guid        guid = new Guid(ShellIIDGuid.IShellItem2);
            int         hrc  = ShellNativeMethods.SHCreateItemFromParsingName(shortcutPath, IntPtr.Zero, ref guid, out shellItem);

            // Get property store
            Guid           psGuid    = new Guid(ShellIIDGuid.IPropertyStore);
            IPropertyStore propStore = null;
            int            hr        = shellItem.GetPropertyStore(
                ShellNativeMethods.GETPROPERTYSTOREFLAGS.GPS_READWRITE,
                ref psGuid,
                out propStore);

            // Get appid property
            PropVariant pv       = new PropVariant();
            PropertyKey AppIdKey = new PropertyKey(new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3"), 5);

            propStore.GetValue(ref AppIdKey, out pv);

            string retrievedAppId;

            if (pv.IsNullOrEmpty)
            {
                retrievedAppId = "";
            }
            else
            {
                retrievedAppId = (string)pv.Value;
            }

            Marshal.ReleaseComObject(propStore);
            pv.Clear();

            return(retrievedAppId);
        }
コード例 #10
0
        private void ApplyNativeSettings(IFileDialog dialog)
        {
            Debug.Assert(dialog != null, "No dialog instance to configure");

            if (parentWindow == IntPtr.Zero)
            {
                if (System.Windows.Application.Current != null && System.Windows.Application.Current.MainWindow != null)
                {
                    parentWindow = (new WindowInteropHelper(System.Windows.Application.Current.MainWindow)).Handle;
                }
            }

            Guid guid = new Guid(ShellIIDGuid.IShellItem2);

            // Apply option bitflags.
            dialog.SetOptions(CalculateNativeDialogOptionFlags());

            // Other property sets.
            if (title != null)
            {
                dialog.SetTitle(title);
            }

            if (initialDirectoryShellContainer != null)
            {
                dialog.SetFolder(((ShellObject)initialDirectoryShellContainer).NativeShellItem);
            }

            if (defaultDirectoryShellContainer != null)
            {
                dialog.SetDefaultFolder(((ShellObject)defaultDirectoryShellContainer).NativeShellItem);
            }

            if (!string.IsNullOrEmpty(initialDirectory))
            {
                // Create a native shellitem from our path
                IShellItem2 initialDirectoryShellItem;
                ShellNativeMethods.SHCreateItemFromParsingName(initialDirectory, IntPtr.Zero, ref guid, out initialDirectoryShellItem);

                // If we get a real shell item back,
                // then use that as the initial folder - otherwise,
                // we'll allow the dialog to revert to the default folder.
                // (OR should we fail loudly?)
                if (initialDirectoryShellItem != null)
                {
                    dialog.SetFolder(initialDirectoryShellItem);
                }
            }

            if (!string.IsNullOrEmpty(defaultDirectory))
            {
                // Create a native shellitem from our path
                IShellItem2 defaultDirectoryShellItem;
                ShellNativeMethods.SHCreateItemFromParsingName(defaultDirectory, IntPtr.Zero, ref guid, out defaultDirectoryShellItem);

                // If we get a real shell item back,
                // then use that as the initial folder - otherwise,
                // we'll allow the dialog to revert to the default folder.
                // (OR should we fail loudly?)
                if (defaultDirectoryShellItem != null)
                {
                    dialog.SetDefaultFolder(defaultDirectoryShellItem);
                }
            }

            // Apply file type filters, if available.
            if (filters.Count > 0 && !filterSet)
            {
                dialog.SetFileTypes(
                    (uint)filters.Count,
                    filters.GetAllFilterSpecs());

                filterSet = true;

                SyncFileTypeComboToDefaultExtension(dialog);
            }

            if (cookieIdentifier != Guid.Empty)
            {
                dialog.SetClientGuid(ref cookieIdentifier);
            }

            // Set the default extension
            if (!string.IsNullOrEmpty(DefaultExtension))
            {
                dialog.SetDefaultExtension(DefaultExtension);
            }

            // Set the default filename
            dialog.SetFileName(DefaultFileName);
        }