Example #1
0
 internal static extern HResult SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string path, IntPtr pbc, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] ref IShellItemPS shellItem);
Example #2
0
        /// <summary>
        /// Advanced initialization of FileObject.  Use this for items in special folders.
        /// </summary>
        /// <param name="parsingName">The shell parsing name of the file.</param>
        /// <param name="isSpecial">Is the file known to be special?</param>
        /// <param name="initialize">True to get file info and load icons.</param>
        /// <param name="iconSize">Default icon size.  This can be changed with the <see cref="IconSize"/> property.</param>
        public FileObject(string parsingName, bool isSpecial, bool initialize, StandardIcons iconSize = StandardIcons.Icon48)
        {
            this.isSpecial = isSpecial;
            filename       = parsingName;
            try
            {
                var     mm   = new MemPtr();
                var     riid = Guid.Parse(ShellIIDGuid.IShellItem);
                HResult res;

                if (this.isSpecial)
                {
                    // let's see if we can parse it.
                    IShellItem   shitem  = null;
                    IShellItemPS pssh    = null;
                    IShellItem2  shitem2 = null;

                    res = NativeShell.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref riid, ref shitem);
                    string fp = null;

                    if (res == HResult.Ok)
                    {
                        NativeShell.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref riid, ref pssh);
                        psshellObj = pssh;

                        riid = Guid.Parse(ShellIIDGuid.IShellItem2);
                        NativeShell.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref riid, ref shitem2);
                        psshellObj2 = shitem2;


                        shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, out mm.handle);
                        fp = (string)mm;

                        ParsingName   = fp;
                        CanonicalName = fp;

                        mm.CoTaskMemFree();

                        shitem.GetDisplayName(ShellItemDesignNameOptions.Normal, out mm.handle);

                        DisplayName = (string)mm;

                        mm.CoTaskMemFree();

                        this.isSpecial = true;

                        if (initialize)
                        {
                            Refresh();
                        }

                        shellObj = shitem;

                        return;
                    }

                    HResult localSHCreateItemFromParsingName()
                    {
                        riid = Guid.Parse(ShellIIDGuid.IShellItem); var ret = NativeShell.SHCreateItemFromParsingName("shell:" + (fp ?? parsingName), IntPtr.Zero, ref riid, ref shitem); return(ret);
                    }

                    res = localSHCreateItemFromParsingName();

                    if (res == HResult.Ok)
                    {
                        shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, out mm.handle);
                        CanonicalName = (string)mm;

                        if (ParsingName is null)
                        {
                            ParsingName = (string)mm;
                        }

                        filename = ParsingName;
                        mm.CoTaskMemFree();


                        shitem.GetDisplayName(ShellItemDesignNameOptions.Normal, out mm.handle);

                        DisplayName = (string)mm;

                        mm.CoTaskMemFree();
                    }

                    shellObj = shitem;
                    shitem   = null;

                    if (!string.IsNullOrEmpty(DisplayName) && !string.IsNullOrEmpty(ParsingName))
                    {
                        this.isSpecial = true;
                        if (initialize)
                        {
                            Refresh(this.iconSize);
                        }
                        return;
                    }
                }

                if (File.Exists(parsingName) == false)
                {
                    if (!this.isSpecial)
                    {
                        throw new FileNotFoundException("File Not Found: " + parsingName);
                    }
                }
                else if (initialize)
                {
                    Refresh(this.iconSize);
                }
            }
            catch
            {
            }
        }