static void Main(string[] args) { NonFileSystemKnownFolder folder = (NonFileSystemKnownFolder) Microsoft.WindowsAPICodePack.Shell.KnownFolderHelper.FromPath( "::{645FF040-5081-101B-9F08-00AA002F954E}"); var desktop = (FileSystemKnownFolder)folder.Parent; //desktop.FolderId {b4bfcc3a-db2c-424c-b029-7fe99a87c641} Console.WriteLine(desktop.CanonicalName); Console.WriteLine(folder); }
static void Main(string[] args) { Console.BufferWidth = 120; Console.WindowWidth = 120; Console.WindowHeight = 50; //IntPtr desktopP; //var desktop = ShellExtension.GetDesktopFolder(out desktopP); //ShellNativeMethods.SHGetDesktopFolder //GetChilds(desktop); //--------------- CreateLib(); ListFolders(); RegisterFolder(); //-------------------- IShellItem s; NonFileSystemKnownFolder sf = (NonFileSystemKnownFolder)ShellObject.FromParsingName(KnownFolders.Computer.ParsingName); IntPtr ppidl; uint flag = 0; SHILCreateFromPath(@"d:\temp", out ppidl, ref flag); var tmp = ShellNativeMethods.SHCreateShellItem(IntPtr.Zero, sf.NativeShellFolder, ppidl, out s); GetFolder(); //DeleteFolder(); Console.WriteLine("Try to get extension again:"); GetFolder(); Console.ReadKey(); }
/// <summary> /// Creates a ShellObject given a native IShellItem interface /// </summary> /// <param name="nativeShellItem"></param> /// <returns>A newly constructed ShellObject object</returns> internal static ShellObject Create(IShellItem nativeShellItem) { // Sanity check Debug.Assert(nativeShellItem != null, "nativeShellItem should not be null"); // Need to make sure we're running on Vista or higher if (!CoreHelpers.RunningOnVista) { throw new PlatformNotSupportedException(LocalizedMessages.ShellObjectFactoryPlatformNotSupported); } // A lot of APIs need IShellItem2, so just keep a copy of it here IShellItem2 nativeShellItem2 = nativeShellItem as IShellItem2; // Get the System.ItemType property string itemType = ShellHelper.GetItemType(nativeShellItem2); if (!string.IsNullOrEmpty(itemType)) { itemType = itemType.ToUpperInvariant(); } // Get some IShellItem attributes ShellNativeMethods.ShellFileGetAttributesOptions sfgao; nativeShellItem2.GetAttributes(ShellNativeMethods.ShellFileGetAttributesOptions.FileSystem | ShellNativeMethods.ShellFileGetAttributesOptions.Folder, out sfgao); // Is this item a FileSystem item? bool isFileSystem = (sfgao & ShellNativeMethods.ShellFileGetAttributesOptions.FileSystem) != 0; // Is this item a Folder? bool isFolder = (sfgao & ShellNativeMethods.ShellFileGetAttributesOptions.Folder) != 0; // Shell Library ShellLibrary shellLibrary = null; // Create the right type of ShellObject based on the above information // 1. First check if this is a Shell Link if (itemType == ".lnk") { return new ShellLink(nativeShellItem2); } // 2. Check if this is a container or a single item (entity) else if (isFolder) { // 3. If this is a folder, check for types: Shell Library, Shell Folder or Search Container if (itemType == ".library-ms" && (shellLibrary = ShellLibrary.FromShellItem(nativeShellItem2, true)) != null) { return shellLibrary; // we already created this above while checking for Library } else if (itemType == ".searchconnector-ms") { return new ShellSearchConnector(nativeShellItem2); } else if (itemType == ".search-ms") { return new ShellSavedSearchCollection(nativeShellItem2); } // 4. It's a ShellFolder if (isFileSystem) { // 5. Is it a (File-System / Non-Virtual) Known Folder if (!IsVirtualKnownFolder(nativeShellItem2)) { //needs to check if it is a known folder and not virtual FileSystemKnownFolder kf = new FileSystemKnownFolder(nativeShellItem2); return kf; } return new ShellFileSystemFolder(nativeShellItem2); } // 5. Is it a (Non File-System / Virtual) Known Folder if (IsVirtualKnownFolder(nativeShellItem2)) { //needs to check if known folder is virtual NonFileSystemKnownFolder kf = new NonFileSystemKnownFolder(nativeShellItem2); return kf; } return new ShellNonFileSystemFolder(nativeShellItem2); } // 6. If this is an entity (single item), check if its filesystem or not if (isFileSystem) { return new ShellFile(nativeShellItem2); } return new ShellNonFileSystemItem(nativeShellItem2); }
/// <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; }