Пример #1
0
        /// <summary>
        /// Given a native KnownFolder (IKnownFolderNative), create the right type of
        /// IKnownFolder object (FileSystemKnownFolder or NonFileSystemKnownFolder)
        /// </summary>
        /// <param name="knownFolderNative">Native Known Folder</param>
        /// <returns></returns>
        private static IKnownFolder GetKnownFolder(IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null, "Native IKnownFolder should not be null.");

            // Get the native IShellItem2 from the native IKnownFolder
            Guid    guid = new Guid(ShellIIDGuid.IShellItem2);
            HResult hr   = knownFolderNative.GetShellItem(0, ref guid, out var shellItem);

            if (!CoreErrorHelper.Succeeded(hr))
            {
                return(null);
            }

            bool isFileSystem = false;

            // If we have a valid IShellItem, try to get the FileSystem attribute.
            if (shellItem != null)
            {
                shellItem.GetAttributes(ShellNativeMethods.ShellFileGetAttributesOptions.FileSystem, out var sfgao);

                // Is this item a FileSystem item?
                isFileSystem = (sfgao & ShellNativeMethods.ShellFileGetAttributesOptions.FileSystem) != 0;
            }

            // If it's FileSystem, create a FileSystemKnownFolder, else NonFileSystemKnownFolder
            if (isFileSystem)
            {
                FileSystemKnownFolder kf = new FileSystemKnownFolder(knownFolderNative);
                return(kf);
            }

            NonFileSystemKnownFolder knownFsFolder = new NonFileSystemKnownFolder(knownFolderNative);

            return(knownFsFolder);
        }
Пример #2
0
        /// <summary>
        /// Populates a structure that contains 
        /// this known folder's properties.
        /// </summary>
        private void GetFolderProperties(IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null);

            KnownFoldersSafeNativeMethods.NativeFolderDefinition nativeFolderDefinition;
            knownFolderNative.GetFolderDefinition(out nativeFolderDefinition);

            try
            {
                knownFolderProperties.category = nativeFolderDefinition.category;
                knownFolderProperties.canonicalName = Marshal.PtrToStringUni(nativeFolderDefinition.name);
                knownFolderProperties.description = Marshal.PtrToStringUni(nativeFolderDefinition.description);
                knownFolderProperties.parentId = nativeFolderDefinition.parentId;
                knownFolderProperties.relativePath = Marshal.PtrToStringUni(nativeFolderDefinition.relativePath);
                knownFolderProperties.parsingName = Marshal.PtrToStringUni(nativeFolderDefinition.parsingName);
                knownFolderProperties.tooltipResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.tooltip);
                knownFolderProperties.localizedNameResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.localizedName);
                knownFolderProperties.iconResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.icon);
                knownFolderProperties.security = Marshal.PtrToStringUni(nativeFolderDefinition.security);
                knownFolderProperties.fileAttributes = (System.IO.FileAttributes)nativeFolderDefinition.attributes;
                knownFolderProperties.definitionOptions = nativeFolderDefinition.definitionOptions;
                knownFolderProperties.folderTypeId = nativeFolderDefinition.folderTypeId;
                knownFolderProperties.folderType = FolderTypes.GetFolderType(knownFolderProperties.folderTypeId);

                bool pathExists;
                knownFolderProperties.path = GetPath(out pathExists, knownFolderNative);
                knownFolderProperties.pathExists = pathExists;
                
                /*
                try
                {
                    knownFolderProperties.redirection = knownFolderNative.GetRedirectionCapabilities();
                }
                catch
                {
                    
                }
                */

                // Turn tooltip, localized name and icon resource IDs 
                // into the actual resources.
                knownFolderProperties.tooltip = CoreHelpers.GetStringResource(knownFolderProperties.tooltipResourceId);
                knownFolderProperties.localizedName = CoreHelpers.GetStringResource(knownFolderProperties.localizedNameResourceId);

                knownFolderProperties.folderId = knownFolderNative.GetId();

            }
            finally
            {
                // Clean up memory. 
                Marshal.FreeCoTaskMem(nativeFolderDefinition.name);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.description);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.relativePath);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.parsingName);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.tooltip);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.localizedName);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.icon);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.security);
            }
        }
Пример #3
0
        /// <summary>
        /// Given a native KnownFolder (IKnownFolderNative), create the right type of
        /// IKnownFolder object (FileSystemKnownFolder or NonFileSystemKnownFolder)
        /// </summary>
        /// <param name="knownFolderNative">Native Known Folder</param>
        /// <returns></returns>
        private static IKnownFolder GetKnownFolder(IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null, "Native IKnownFolder should not be null.");

            // Get the native IShellItem2 from the native IKnownFolder
            IShellItem2 shellItem;
            Guid guid = new Guid(ShellIIDGuid.IShellItem2);
            HResult hr = knownFolderNative.GetShellItem(0, ref guid, out shellItem);

            if (!CoreErrorHelper.Succeeded(hr)) { return null; }

            bool isFileSystem = false;

            // If we have a valid IShellItem, try to get the FileSystem attribute.
            if (shellItem != null)
            {
                ShellNativeMethods.ShellFileGetAttributesOptions sfgao;
                shellItem.GetAttributes(ShellNativeMethods.ShellFileGetAttributesOptions.FileSystem, out sfgao);

                // Is this item a FileSystem item?
                isFileSystem = (sfgao & ShellNativeMethods.ShellFileGetAttributesOptions.FileSystem) != 0;
            }

            // If it's FileSystem, create a FileSystemKnownFolder, else NonFileSystemKnownFolder
            if (isFileSystem)
            {
                FileSystemKnownFolder kf = new FileSystemKnownFolder(knownFolderNative);
                return kf;
            }

            NonFileSystemKnownFolder knownFsFolder = new NonFileSystemKnownFolder(knownFolderNative);
            return knownFsFolder;
        }
Пример #4
0
        /// <summary>
        /// Gets the path of this this known folder.
        /// </summary>
        /// <param name="fileExists">
        /// Returns false if the folder is virtual, or a boolean
        /// value that indicates whether this known folder exists.
        /// </param>
        /// <param name="knownFolderNative">Native IKnownFolder reference</param>
        /// <returns>
        /// A <see cref="System.String"/> containing the path, or <see cref="string.Empty"/> if this known folder does not exist.
        /// </returns>
        private string GetPath(out bool fileExists, IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null);

            string kfPath = string.Empty;

            fileExists = true;

            // Virtual folders do not have path.
            if (knownFolderProperties.category == FolderCategory.Virtual)
            {
                fileExists = false;
                return(kfPath);
            }

            try
            {
                kfPath = knownFolderNative.GetPath(0);
            }
            catch (System.IO.FileNotFoundException)
            {
                fileExists = false;
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                fileExists = false;
            }

            return(kfPath);
        }
Пример #5
0
        /// <summary>
        /// Given a native KnownFolder (IKnownFolderNative), create the right type of
        /// IKnownFolder object (FileSystemKnownFolder or NonFileSystemKnownFolder)
        /// </summary>
        /// <param name="knownFolderNative">Native Known Folder</param>
        /// <returns></returns>
        private static IKnownFolder GetKnownFolder(IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null, "Native IKnownFolder should not be null.");

            // Get the native IShellItem2 from the native IKnownFolder
            IShellItem shellItem;
            Guid guid = new Guid(InterfaceGuids.IShellItem);
            HResult hr = knownFolderNative.GetShellItem(0, ref guid, out shellItem);

            if (hr != HResult.S_OK) return null;

            bool isFileSystem = false;

            // If we have a valid IShellItem, try to get the FileSystem attribute.
            if (shellItem != null)
            {
                SFGAO sfgao;
                shellItem.GetAttributes(SFGAO.FILESYSTEM, out sfgao);

                // Is this item a FileSystem item?
                isFileSystem = (sfgao & SFGAO.FILESYSTEM) != 0;
            }

            // If it's FileSystem, create a FileSystemKnownFolder, else NonFileSystemKnownFolder
            if (isFileSystem)
            {
                return FileSystemKnownFolder.Create(knownFolderNative);
            }

            var knownFsFolder = new NonFileSystemKnownFolder(knownFolderNative);
            return knownFsFolder;
        }
Пример #6
0
        /// <summary>
        /// Populates a structure that contains
        /// this known folder's properties.
        /// </summary>
        private void GetFolderProperties(IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null);

            KnownFoldersSafeNativeMethods.NativeFolderDefinition nativeFolderDefinition;
            knownFolderNative.GetFolderDefinition(out nativeFolderDefinition);

            try
            {
                knownFolderProperties.category                = nativeFolderDefinition.category;
                knownFolderProperties.canonicalName           = Marshal.PtrToStringUni(nativeFolderDefinition.name);
                knownFolderProperties.description             = Marshal.PtrToStringUni(nativeFolderDefinition.description);
                knownFolderProperties.parentId                = nativeFolderDefinition.parentId;
                knownFolderProperties.relativePath            = Marshal.PtrToStringUni(nativeFolderDefinition.relativePath);
                knownFolderProperties.parsingName             = Marshal.PtrToStringUni(nativeFolderDefinition.parsingName);
                knownFolderProperties.tooltipResourceId       = Marshal.PtrToStringUni(nativeFolderDefinition.tooltip);
                knownFolderProperties.localizedNameResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.localizedName);
                knownFolderProperties.iconResourceId          = Marshal.PtrToStringUni(nativeFolderDefinition.icon);
                knownFolderProperties.security                = Marshal.PtrToStringUni(nativeFolderDefinition.security);
                knownFolderProperties.fileAttributes          = (System.IO.FileAttributes)nativeFolderDefinition.attributes;
                knownFolderProperties.definitionOptions       = nativeFolderDefinition.definitionOptions;
                knownFolderProperties.folderTypeId            = nativeFolderDefinition.folderTypeId;
                knownFolderProperties.folderType              = FolderTypes.GetFolderType(knownFolderProperties.folderTypeId);

                bool pathExists;
                knownFolderProperties.path       = GetPath(out pathExists, knownFolderNative);
                knownFolderProperties.pathExists = pathExists;

                /*
                 * try
                 * {
                 *  knownFolderProperties.redirection = knownFolderNative.GetRedirectionCapabilities();
                 * }
                 * catch
                 * {
                 *
                 * }
                 */

                // Turn tooltip, localized name and icon resource IDs
                // into the actual resources.
                knownFolderProperties.tooltip       = CoreHelpers.GetStringResource(knownFolderProperties.tooltipResourceId);
                knownFolderProperties.localizedName = CoreHelpers.GetStringResource(knownFolderProperties.localizedNameResourceId);

                knownFolderProperties.folderId = knownFolderNative.GetId();
            }
            finally
            {
                // Clean up memory.
                Marshal.FreeCoTaskMem(nativeFolderDefinition.name);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.description);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.relativePath);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.parsingName);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.tooltip);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.localizedName);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.icon);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.security);
            }
        }
Пример #7
0
        /*
         *      /// <summary>
         *      /// Returns a known folder given its shell path, such as <c>C:\users\public\documents</c> or
         *      /// <c>::{645FF040-5081-101B-9F08-00AA002F954E}</c> for the Recycle Bin.
         *      /// </summary>
         *      /// <param name="path">The path for the requested known folder; either a physical path or a virtual path.</param>
         *      /// <returns>A known folder representing the specified name.</returns>
         *      public static IKnownFolder FromPath(string path) {
         *              return KnownFolderHelper.FromParsingName(path);
         *      }
         */

        /// <summary>
        /// Returns a known folder given its shell namespace parsing name, such as
        /// <c>::{645FF040-5081-101B-9F08-00AA002F954E}</c> for the Recycle Bin.
        /// </summary>
        /// <param name="parsingName">The parsing name (or path) for the requested known folder.</param>
        /// <returns>A known folder representing the specified name.</returns>
        /// <exception cref="System.ArgumentException">Thrown if the given parsing name is invalid.</exception>
        ///
        public static IKnownFolder FromParsingNameOnly(string parsingName)
        {
            var k = new KnownFolderManagerClass();
            IKnownFolderNative folder = null;

            k.FindFolderFromPath(parsingName, 0, out folder);
            return((IKnownFolder)folder);
        }
Пример #8
0
		internal FileSystemKnownFolder(IKnownFolderNative kf) {
			Debug.Assert(kf != null);
			knownFolderNative = kf;

			// Set the native shell item
			// and set it on the base class (ShellObject)
			Guid guid = new Guid(ShellIIDGuid.IShellItem2);
			knownFolderNative.GetShellItem(0, ref guid, out nativeShellItem);
		}
Пример #9
0
        /// <summary>
        /// Returns a known folder given its shell namespace parsing name, such as
        /// <c>::{645FF040-5081-101B-9F08-00AA002F954E}</c> for the Recycle Bin.
        /// </summary>
        /// <param name="parsingName">The parsing name (or path) for the requested known folder.</param>
        /// <returns>A known folder representing the specified name.</returns>
        /// <exception cref="System.ArgumentException">Thrown if the given parsing name is invalid.</exception>
        public static IKnownFolder FromParsingName(string parsingName)
        {
            if (parsingName == null)
            {
                throw new ArgumentNullException(nameof(parsingName));
            }

            IntPtr pidl  = IntPtr.Zero;
            IntPtr pidl2 = IntPtr.Zero;

            try
            {
                pidl = ShellHelper.PidlFromParsingName(parsingName);

                if (pidl == IntPtr.Zero)
                {
                    throw new ArgumentException(LocalizedMessages.KnownFolderParsingName, nameof(parsingName));
                }

                // It's probably a special folder, try to get it
                IKnownFolderNative knownFolderNative = KnownFolderHelper.FromPIDL(pidl);
                if (knownFolderNative != null)
                {
                    IKnownFolder kf = KnownFolderHelper.GetKnownFolder(knownFolderNative);
                    if (kf == null)
                    {
                        throw new ArgumentException(LocalizedMessages.KnownFolderParsingName, nameof(parsingName));
                    }

                    return(kf);
                }

                // No physical storage was found for this known folder
                // We'll try again with a different name

                // try one more time with a trailing \0
                pidl2 = ShellHelper.PidlFromParsingName(parsingName.PadRight(1, '\0'));

                if (pidl2 == IntPtr.Zero)
                {
                    throw new ArgumentException(LocalizedMessages.KnownFolderParsingName, nameof(parsingName));
                }

                IKnownFolder kf2 = KnownFolderHelper.GetKnownFolder(KnownFolderHelper.FromPIDL(pidl));
                if (kf2 == null)
                {
                    throw new ArgumentException(LocalizedMessages.KnownFolderParsingName, nameof(parsingName));
                }

                return(kf2);
            }
            finally
            {
                ShellNativeMethods.ILFree(pidl);
                ShellNativeMethods.ILFree(pidl2);
            }
        }
        internal NonFileSystemKnownFolder(IKnownFolderNative kf)
        {
            Debug.Assert(kf != null);
            knownFolderNative = kf;

            // Set the native shell item and set it on the base class (ShellObject)
            var guid = new Guid(ShellIIDGuid.IShellItem2);

            knownFolderNative.GetShellItem(0, ref guid, out nativeShellItem);
        }
        internal NonFileSystemKnownFolder(IKnownFolderNative kf)
        {
            Debug.Assert(kf != null);
            knownFolderNative = kf;

            // Set the native shell item
            // and set it on the base class (ShellObject)

            Guid guid = new Guid(InterfaceGuids.IShellItem);

            knownFolderNative.GetShellItem(0, ref guid, out nativeShellItem);
            base.m_ComInterface = nativeShellItem;
        }
Пример #12
0
 /// <summary>
 /// Class constructor fron internal structure.
 /// </summary>
 /// <param name="knownFolderNative"></param>
 /// <param name="nativeFolderDefinition"></param>
 public KnownFolderProperties(IKnownFolderNative knownFolderNative,
                              NativeFolderDefinition nativeFolderDefinition)
     : this()
 {
     try
     {
         Init(knownFolderNative, nativeFolderDefinition);
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.ToString());
     }
 }
Пример #13
0
        /// <summary>
        /// Source: http://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-P
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed == false)
            {
                if (disposing == true)
                {
                    if (Obj != null)
                    {
                        try
                        {
                            Marshal.ReleaseComObject(Obj);
                        }
                        catch
                        {
                            // Catch this, just in case, to complete disposable on error
                        }
                        finally
                        {
                            Obj = null;
                        }
                    }

                    if (_intPtrKnownFolder != IntPtr.Zero)
                    {
                        try
                        {
                            Marshal.Release(_intPtrKnownFolder);
                        }
                        catch
                        {
                            // Catch this, just in case, to complete disposable on error
                        }
                        finally
                        {
                            _intPtrKnownFolder = IntPtr.Zero;
                        }
                    }

                    GC.SuppressFinalize(this);
                }

                // There are no unmanaged resources to release, but
                // if we add them, they need to be released here.
            }

            _disposed = true;

            //// If it is available, make the call to the
            //// base class's Dispose(Boolean) method
            ////base.Dispose(disposing);
        }
Пример #14
0
        // This is a work around for the STA thread bug.  This will execute the call on a non-sta thread, then return the result
        private static bool IsVirtualKnownFolder(IShellItem2 nativeShellItem2)
        {
            IntPtr pidl = IntPtr.Zero;

            try
            {
                IKnownFolderNative nativeFolder = null;
                KnownFoldersSafeNativeMethods.NativeFolderDefinition definition = new KnownFoldersSafeNativeMethods.NativeFolderDefinition();

                // We found a bug where the enumeration of shell folders was
                // not reliable when called from a STA thread - it would return
                // different results the first time vs the other times.
                //
                // This is a work around.  We call FindFolderFromIDList on a
                // worker MTA thread instead of the main STA thread.
                //
                // Ultimately, it would be a very good idea to replace the 'getting shell object' logic
                // to get a list of pidl's in 1 step, then look up their information in a 2nd, rather than
                // looking them up as we get them.  This would replace the need for the work around.
                object padlock = new object();
                lock (padlock)
                {
                    IntPtr unknown = Marshal.GetIUnknownForObject(nativeShellItem2);

                    ThreadPool.QueueUserWorkItem(obj =>
                    {
                        lock (padlock)
                        {
                            pidl = ShellHelper.PidlFromUnknown(unknown);

                            new KnownFolderManagerClass().FindFolderFromIDList(pidl, out nativeFolder);

                            if (nativeFolder != null)
                            {
                                nativeFolder.GetFolderDefinition(out definition);
                            }

                            Monitor.Pulse(padlock);
                        }
                    });

                    Monitor.Wait(padlock);
                }

                return(nativeFolder != null && definition.category == FolderCategory.Virtual);
            }
            finally
            {
                ShellNativeMethods.ILFree(pidl);
            }
        }
Пример #15
0
        /// <summary>
        /// Initializr objrct from <paramref name="intPtrKnownFolderNative"/>
        /// or <paramref name="iknownfolder"/>. You should use only one
        /// parameter here and set the other to default if necessary.
        /// </summary>
        /// <param name="intPtrKnownFolderNative"></param>
        /// <param name="iknownfolder"></param>
        protected void InitObject(IntPtr intPtrKnownFolderNative,
                                  IKnownFolderNative iknownfolder = null)
        {
            _intPtrKnownFolder = intPtrKnownFolderNative;

            if (_intPtrKnownFolder != IntPtr.Zero)
            {
                Obj = (IKnownFolderNative)Marshal.GetTypedObjectForIUnknown(intPtrKnownFolderNative, typeof(IKnownFolderNative));
            }
            else
            {
                Obj = iknownfolder;
            }
        }
Пример #16
0
        /// <summary>
        /// Populates an object that contains a known folder's properties.
        /// </summary>
        public static IKnownFolderProperties GetFolderProperties(IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null);

            NativeFolderDefinition nativeFolderDefinition = default(NativeFolderDefinition);

            try
            {
                knownFolderNative.GetFolderDefinition(out nativeFolderDefinition);

                KnownFolderProperties knownFolderProperties =
                    new KnownFolderProperties(knownFolderNative,
                                              nativeFolderDefinition);

                // Error handling - Empty Pidl indicates non-existing local folder
                // Let's not worry about non-exisitng local 'known' folders
                if (knownFolderProperties.PidlIdList.Size == 0 &&
                    knownFolderProperties.FolderId != new Guid(KF_ID.ID_FOLDERID_Desktop))
                {
                    return(default(KnownFolderProperties));
                }

                return(knownFolderProperties);
            }
            catch
            {
                return(default(KnownFolderProperties));
            }
            finally
            {
                // Clean up memory.
                ////FolderCategory category
                Marshal.FreeCoTaskMem(nativeFolderDefinition.name);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.description);

                ////Guid parentId;

                Marshal.FreeCoTaskMem(nativeFolderDefinition.relativePath);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.parsingName);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.tooltip);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.localizedName);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.icon);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.security);

                ////UInt32 attributes;
                ////DefinitionOptions definitionOptions;
                ////Guid folderTypeId;
            }
        }
Пример #17
0
        private static IKnownFolderNative GetNativeKnownFolder(IShellItem nativeShellItem, out bool isVirtual)
        {
            IntPtr pidl = IntPtr.Zero;

            try
            {
                // Get the PIDL for the ShellItem
                pidl = ShellHelper.PidlFromShellItem(nativeShellItem);

                if (pidl == IntPtr.Zero)
                {
                    isVirtual = false;
                    return(null);
                }

                IKnownFolderNative knownFolderNative = KnownFolderHelper.FromPIDL(pidl);

                if (knownFolderNative != null)
                {
                    // If we have a valid IKnownFolder, try to get its category
                    KnownFoldersSafeNativeMethods.NativeFolderDefinition nativeFolderDefinition;
                    knownFolderNative.GetFolderDefinition(out nativeFolderDefinition);

                    // Get the category type and see if it's virtual
                    if (nativeFolderDefinition.category == FolderCategory.Virtual)
                    {
                        isVirtual = true;
                    }
                    else
                    {
                        isVirtual = false;
                    }

                    return(knownFolderNative);
                }
                else
                {
                    // KnownFolderHelper.FromPIDL could not create a valid KnownFolder from the given PIDL.
                    // Return null to indicate the given IShellItem is not a KnownFolder. Also set our out parameter
                    // to default value.
                    isVirtual = false;
                    return(null);
                }
            }
            finally
            {
                ShellNativeMethods.ILFree(pidl);
            }
        }
Пример #18
0
        /// <summary>
        /// Release resources
        /// </summary>
        /// <param name="disposing">Indicates that this mothod is being called from Dispose() rather than the finalizer.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                knownFolderSettings = null;
            }

            if (knownFolderNative != null)
            {
                Marshal.ReleaseComObject(knownFolderNative);
                knownFolderNative = null;
            }

            base.Dispose(disposing);
        }
Пример #19
0
        /// <summary>
        /// Gets the path of this this known folder or null if known folder is not
        /// available or does not have a representation in the file system.
        /// </summary>
        /// <param name="fileExists">
        /// Returns false if the folder is virtual, or a boolean
        /// value that indicates whether this known folder exists.
        /// </param>
        /// <param name="knownFolderNative">Native IKnownFolder reference</param>
        /// <returns>
        /// A <see cref="System.String"/> containing the path, or <see cref="System.String.Empty"/>
        /// if this known folder does not exist.
        /// </returns>
        public static string GetPath(out bool fileExists, IKnownFolderNative knownFolderNative)
        {
            string kfPath = null;

            fileExists = true;

            if (knownFolderNative == null)
            {
                fileExists = false;
                return(null);
            }

            // Virtual folders do not have path.
            if (knownFolderNative.GetCategory() == FolderCategory.Virtual)
            {
                fileExists = false;
                return(kfPath);
            }

            IntPtr ptrPath = default(IntPtr);

            try
            {
                knownFolderNative.GetPath(0, out ptrPath);

                if (ptrPath != default(IntPtr))
                {
                    kfPath = Marshal.PtrToStringUni(ptrPath);
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                fileExists = false;
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                fileExists = false;
            }
            finally
            {
                if (ptrPath != default(IntPtr))
                {
                    Marshal.FreeCoTaskMem(ptrPath);
                }
            }

            return(kfPath);
        }
        //IShellItem nativeShellItem;

        internal static FileSystemKnownFolder Create(IKnownFolderNative kf)
        {
            Debug.Assert(kf != null);
            var _knownFolderNative = kf;
            IShellItem _nativeShellItem;

            // Set the native shell item
            // and set it on the base class (ShellObject)

            Guid guid = new Guid(InterfaceGuids.IShellItem);
            _knownFolderNative.GetShellItem(0, ref guid, out _nativeShellItem);

            var FS = new FileSystemKnownFolder(_nativeShellItem);
            FS.knownFolderNative = _knownFolderNative;

            return FS;
        }
Пример #21
0
		/// <summary>
		/// Gets the path of this this known folder.
		/// </summary>
		/// <param name="fileExists">
		/// Returns false if the folder is virtual, or a boolean
		/// value that indicates whether this known folder exists.
		/// </param>
		/// <param name="knownFolderNative">Native IKnownFolder reference</param>
		/// <returns>
		/// A <see cref="System.String"/> containing the path, or <see cref="System.String.Empty"/> if this known folder does not exist.
		/// </returns>
		private string GetPath(out bool fileExists, IKnownFolderNative knownFolderNative) {
			Debug.Assert(knownFolderNative != null);

			string kfPath = string.Empty;
			fileExists = true;

			// Virtual folders do not have path.
			if (knownFolderProperties.category == FolderCategory.Virtual) {
				fileExists = false;
				return kfPath;
			}

			try {
				kfPath = knownFolderNative.GetPath(0);
			} catch (System.IO.FileNotFoundException) {
				fileExists = false;
			} catch (System.IO.DirectoryNotFoundException) {
				fileExists = false;
			}

			return kfPath;
		}
Пример #22
0
        /// <summary>
        /// Given a native KnownFolder (IKnownFolderNative), create the right type of
        /// IKnownFolder object (FileSystemKnownFolder or NonFileSystemKnownFolder)
        /// </summary>
        /// <param name="knownFolderNative">Native Known Folder</param>
        /// <returns></returns>
        private static IKnownFolder GetKnownFolder(IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null, "Native IKnownFolder should not be null.");

            // Get the native IShellItem2 from the native IKnownFolder
            IShellItem2 shellItem;
            Guid        guid = new Guid(ShellIIDGuid.IShellItem2);
            HRESULT     hr   = knownFolderNative.GetShellItem(0, ref guid, out shellItem);

            if (!CoreErrorHelper.Succeeded((int)hr))
            {
                return(null);
            }

            bool isFileSystem = false;

            // If we have a valid IShellItem, try to get the FileSystem attribute.
            if (shellItem != null)
            {
                ShellNativeMethods.SFGAO sfgao;
                shellItem.GetAttributes(ShellNativeMethods.SFGAO.SFGAO_FILESYSTEM, out sfgao);

                // Is this item a FileSystem item?
                isFileSystem = (sfgao & ShellNativeMethods.SFGAO.SFGAO_FILESYSTEM) != 0;
            }

            // If it's FileSystem, create a FileSystemKnownFolder, else NonFileSystemKnownFolder
            if (isFileSystem)
            {
                FileSystemKnownFolder kf = new FileSystemKnownFolder(knownFolderNative);
                return(kf);
            }
            else
            {
                NonFileSystemKnownFolder kf = new NonFileSystemKnownFolder(knownFolderNative);
                return(kf);
            }
        }
        /// <summary>
        /// Given a native KnownFolder (IKnownFolderNative), create the right type of
        /// IKnownFolder object (FileSystemKnownFolder or NonFileSystemKnownFolder)
        /// </summary>
        /// <param name="knownFolderNative">Native Known Folder</param>
        /// <returns></returns>
        private static IKnownFolder GetKnownFolder(IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null, "Native IKnownFolder should not be null.");

            // Get the native IShellItem2 from the native IKnownFolder
            IShellItem shellItem;
            Guid       guid = new Guid(InterfaceGuids.IShellItem);
            HResult    hr   = knownFolderNative.GetShellItem(0, ref guid, out shellItem);

            if (hr != HResult.S_OK)
            {
                return(null);
            }

            bool isFileSystem = false;

            // If we have a valid IShellItem, try to get the FileSystem attribute.
            if (shellItem != null)
            {
                SFGAO sfgao =
                    shellItem.GetAttributes(SFGAO.FILESYSTEM);

                // Is this item a FileSystem item?
                isFileSystem = (sfgao & SFGAO.FILESYSTEM) != 0;
            }

            // If it's FileSystem, create a FileSystemKnownFolder, else NonFileSystemKnownFolder
            if (isFileSystem)
            {
                FileSystemKnownFolder kf = new FileSystemKnownFolder(knownFolderNative);
                return(kf);
            }

            NonFileSystemKnownFolder knownFsFolder = new NonFileSystemKnownFolder(knownFolderNative);

            return(knownFsFolder);
        }
 public virtual extern HResult FindFolderFromIDList(IntPtr pidl, [Out, MarshalAs(UnmanagedType.Interface)] out IKnownFolderNative knownFolder);
 public virtual extern void FindFolderFromPath(
     [In, MarshalAs(UnmanagedType.LPWStr)] string path,
     [In] int mode,
     [Out, MarshalAs(UnmanagedType.Interface)] out IKnownFolderNative knownFolder);
 public virtual extern void GetFolderByName(
     string canonicalName,
     [Out, MarshalAs(UnmanagedType.Interface)] out IKnownFolderNative knownFolder);
 public virtual extern HResult GetFolder(
     [In, MarshalAs(UnmanagedType.LPStruct)] Guid id,
     [Out, MarshalAs(UnmanagedType.Interface)]
     out IKnownFolderNative knownFolder);
Пример #28
0
 /// <summary>
 /// Class constructor from <see cref="IKnownFolderNative"/> interface.
 /// </summary>
 /// <param name="knownFolderNative"></param>
 public KnownFolderNative(IKnownFolderNative knownFolderNative)
     : this()
 {
     InitObject(IntPtr.Zero, knownFolderNative);
 }
        /// <summary>
        /// Populates a structure that contains 
        /// this known folder's properties.
        /// </summary>
        private void GetFolderProperties(IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null);

            KnownFoldersSafeNativeMethods.NativeFolderDefinition nativeFolderDefinition;
            knownFolderNative.GetFolderDefinition(out nativeFolderDefinition);

            try
            {
                knownFolderProperties.category = nativeFolderDefinition.category;
                knownFolderProperties.canonicalName = Marshal.PtrToStringUni(nativeFolderDefinition.name);
                knownFolderProperties.description = Marshal.PtrToStringUni(nativeFolderDefinition.description);
                knownFolderProperties.parentId = nativeFolderDefinition.parentId;
                knownFolderProperties.relativePath = Marshal.PtrToStringUni(nativeFolderDefinition.relativePath);
                knownFolderProperties.parsingName = Marshal.PtrToStringUni(nativeFolderDefinition.parsingName);
                knownFolderProperties.tooltipResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.tooltip);
                knownFolderProperties.localizedNameResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.localizedName);
                knownFolderProperties.iconResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.icon);
                knownFolderProperties.security = Marshal.PtrToStringUni(nativeFolderDefinition.security);
                knownFolderProperties.fileAttributes = (System.IO.FileAttributes)nativeFolderDefinition.attributes;
                knownFolderProperties.definitionOptions = nativeFolderDefinition.definitionOptions;
                knownFolderProperties.folderTypeId = nativeFolderDefinition.folderTypeId;
                knownFolderProperties.folderType = FolderTypes.GetFolderType(knownFolderProperties.folderTypeId);

                bool pathExists;

                knownFolderProperties.path = GetPath(out pathExists, knownFolderNative);
                knownFolderProperties.pathExists = pathExists;

                knownFolderProperties.redirection = knownFolderNative.GetRedirectionCapabilities();

                // Turn tooltip, localized name and icon resource IDs
                // into the actual resources.
                try
                {
                    knownFolderProperties.tooltip = CoreHelpers.GetStringResource(knownFolderProperties.tooltipResourceId);
                }
                catch(Win32Exception)
                {
                    // The resource ID isn't valid. This is most likely because there is no resource
                    // defined for this KnownFolder. We should keep the default value (i.e. empty string)
                    // and not throw an exception or set it to null.
                }

                try
                {
                    knownFolderProperties.localizedName = CoreHelpers.GetStringResource(knownFolderProperties.localizedNameResourceId);
                }
                catch(Win32Exception)
                {
                    // The resource ID isn't valid. This is most likely because there is no resource
                    // defined for this KnownFolder. We should keep the default value (i.e. empty string)
                    // and not throw an exception or set it to null.
                }

                knownFolderProperties.folderId = knownFolderNative.GetId();

            }
            finally
            {
                // Clean up memory.
                Marshal.FreeCoTaskMem(nativeFolderDefinition.name);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.description);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.relativePath);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.parsingName);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.tooltip);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.localizedName);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.icon);
                Marshal.FreeCoTaskMem(nativeFolderDefinition.security);
            }
        }
Пример #30
0
        /// <summary>
        /// Returns a known folder given its shell path, such as <c>C:\users\public\documents</c> or
        /// <c>::{645FF040-5081-101B-9F08-00AA002F954E}</c> for the Recycle Bin.
        /// </summary>
        /// <param name="parsingName">The path for the requested known folder; either a physical path or a virtual path.</param>
        /// <returns>A known folder representing the specified name.</returns>
        public static IKnownFolder FromParsingName(string parsingName)
        {
            IntPtr pidl = IntPtr.Zero, pidl2 = IntPtr.Zero;

            if (parsingName == null)
            {
                throw new ArgumentNullException("parsingName");
            }

            try
            {
                pidl = Shell32.PidlFromParsingName(parsingName);

                if (pidl == IntPtr.Zero)
                {
                    throw new ArgumentException("Parsing Name", "parsingName");
                }

                // It's probably a special folder, try to get it
                IKnownFolderNative knownFolderNative = KnownFolderHelper.FromPIDL(pidl);
                if (knownFolderNative != null)
                {
                    IKnownFolder kf = KnownFolderHelper.GetKnownFolder(knownFolderNative);
                    if (kf == null)
                    {
                        throw new ArgumentException("Parsing Name", "parsingName");
                    }
                    else
                    {
                        return(kf);
                    }
                }

                // No physical storage was found for this known folder
                // We'll try again with a different name

                // try one more time with a trailing \0
                pidl2 = Shell32.PidlFromParsingName(parsingName.PadRight(1, '\0'));

                if (pidl2 == IntPtr.Zero)
                {
                    throw new ArgumentException("Parsing Name", "parsingName");
                }

                IKnownFolderNative nativeKnownFolder = KnownFolderHelper.FromPIDL(pidl);
                IKnownFolder       kf2 = null;
                if (nativeKnownFolder != null)
                {
                    kf2 = KnownFolderHelper.GetKnownFolder(nativeKnownFolder);

                    if (kf2 == null)
                    {
                        throw new ArgumentException("Parsing Name", "parsingName");
                    }
                }

                return(kf2);
            }
            finally
            {
                Shell32.ILFree(pidl);
                Shell32.ILFree(pidl2);
            }
        }
Пример #31
0
 public KnownFolderSettings(IKnownFolderNative knownFolderNative)
 {
     GetFolderProperties(knownFolderNative);
 }
 /// <summary>
 /// Returns the native known folder (IKnownFolderNative) given a PID list
 /// </summary>
 /// <param name="pidl"></param>
 /// <returns></returns>
 internal static IKnownFolderNative FromPIDL(in IntPtr pidl) => new KnownFolderManagerClass().FindFolderFromIDList(pidl, out IKnownFolderNative knownFolder) == HResult.Ok ? knownFolder : null;
Пример #33
0
 internal KnownFolderSettings(IKnownFolderNative knownFolderNative) => GetFolderProperties(knownFolderNative);
        /// <summary>
        /// Release resources
        /// </summary>
        /// <param name="disposing">Indicates that this mothod is being called from Dispose() rather than the finalizer.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                knownFolderSettings = null;
            }

            if (knownFolderNative != null)
            {
                Marshal.ReleaseComObject(knownFolderNative);
                knownFolderNative = null;
            }

            base.Dispose(disposing);
        }
Пример #35
0
        private void Init(IKnownFolderNative knownFolderNative,
                          NativeFolderDefinition nativeFolderDefinition)
        {
            this.Name = Marshal.PtrToStringUni(nativeFolderDefinition.name);

            this.Category                = nativeFolderDefinition.category;
            this.CanonicalName           = Marshal.PtrToStringUni(nativeFolderDefinition.name);
            this.Description             = Marshal.PtrToStringUni(nativeFolderDefinition.description);
            this.ParentId                = nativeFolderDefinition.parentId;
            this.RelativePath            = Marshal.PtrToStringUni(nativeFolderDefinition.relativePath);
            this.ParsingName             = Marshal.PtrToStringUni(nativeFolderDefinition.parsingName);
            this.TooltipResourceId       = Marshal.PtrToStringUni(nativeFolderDefinition.tooltip);
            this.LocalizedNameResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.localizedName);
            this.IconResourceId          = Marshal.PtrToStringUni(nativeFolderDefinition.icon);
            this.Security                = Marshal.PtrToStringUni(nativeFolderDefinition.security);
            this.FileAttributes          = (System.IO.FileAttributes)nativeFolderDefinition.attributes;
            this.DefinitionOptions       = nativeFolderDefinition.definitionOptions;
            this.FolderTypeId            = nativeFolderDefinition.folderTypeId;
            ////knownFolderProperties.folderType = FolderTypes.GetFolderType(knownFolderProperties.folderTypeId);

            this.Redirection = knownFolderNative.GetRedirectionCapabilities();

            // Turn tooltip, localized name and icon resource IDs
            // into the actual resources.
            this.Tooltip       = GetStringResource(this.TooltipResourceId);
            this.LocalizedName = GetStringResource(this.LocalizedNameResourceId);

            this.FolderId = knownFolderNative.GetId();

            bool pathExists = false;

            this.IsExistsInFileSystem = false;
            this.IsPathExists         = false;
            this.PidlIdList           = null;

            using (var kfObj = new KnownFolderNative(knownFolderNative))
            {
                if (kfObj != null)
                {
                    try
                    {
                        bool?isExists = kfObj.IsFileSystem();

                        if (isExists != null)
                        {
                            if (isExists == true)
                            {
                                this.IsExistsInFileSystem = true;
                            }

                            this.Path         = KnownFolderHelper.GetPath(out pathExists, knownFolderNative);
                            this.IsPathExists = pathExists;
                        }
                    }
                    catch
                    {
                        // Catch this just in case
                    }

                    try
                    {
                        this.PidlIdList = kfObj.KnownFolderToIdList();
                    }
                    catch
                    {
                        // Catch this just in case
                    }
                }
            }

            // Load Icon ResourceId from Icon Resource helper (if not already present)
            if (IsIconResourceIdValid(IconResourceId) == false && PidlIdList != null)
            {
                if (PidlIdList.Size == 0 && this.FolderId == new Guid(KF_ID.ID_FOLDERID_Desktop))
                {
                    IconResourceId = IconHelper.FromPidl(PidlIdList, true, false);
                }
                else
                {
                    if (PidlIdList.Size != 0)
                    {
                        IconResourceId = IconHelper.FromPidl(PidlIdList, true, false);
                    }
                }
            }
        }
Пример #36
0
 public KnownFolderSettings(IKnownFolderNative knownFolderNative)
 {
     GetFolderProperties(knownFolderNative);
 }
Пример #37
0
 internal KnownFolderSettings(IKnownFolderNative knownFolderNative)
 {
     GetFolderProperties(knownFolderNative);
 }
Пример #38
0
        /// <summary>
        /// Given a native KnownFolder (IKnownFolderNative), create the right type of
        /// IKnownFolder object (FileSystemKnownFolder or NonFileSystemKnownFolder)
        /// </summary>
        /// <param name="knownFolderNative">Native Known Folder</param>
        /// <returns></returns>
        private static IKnownFolder GetKnownFolder(IKnownFolderNative knownFolderNative)
        {
            Debug.Assert(knownFolderNative != null, "Native IKnownFolder should not be null.");

            // Get the native IShellItem2 from the native IKnownFolder
            IShellItem2 shellItem;
            Guid guid = new Guid(ShellIIDGuid.IShellItem2);
            knownFolderNative.GetShellItem(0, ref guid, out shellItem);

            bool isFileSystem = false;

            // If we have a valid IShellItem, try to get the FileSystem attribute.
            if (shellItem != null)
            {
                ShellNativeMethods.SFGAO sfgao;
                shellItem.GetAttributes(ShellNativeMethods.SFGAO.SFGAO_FILESYSTEM, out sfgao);

                // Is this item a FileSystem item?
                isFileSystem = (sfgao & ShellNativeMethods.SFGAO.SFGAO_FILESYSTEM) != 0;
            }

            // If it's FileSystem, create a FileSystemKnownFolder, else NonFileSystemKnownFolder
            if (isFileSystem)
            {
                FileSystemKnownFolder kf = new FileSystemKnownFolder(knownFolderNative);
                return kf;
            }
            else
            {
                NonFileSystemKnownFolder kf = new NonFileSystemKnownFolder(knownFolderNative);
                return kf;
            }
        }