Exemplo n.º 1
0
        /// <summary>
        /// Create a new file-system-linked directory object
        /// </summary>
        /// <param name="path"></param>
        public DirectoryObject(string parsingName, bool isSpecial = false, bool initialize = true, StandardIcons iconSize = StandardIcons.Icon48)
        {
            if (string.IsNullOrEmpty(parsingName))
            {
                throw new ArgumentNullException(nameof(Path), "Path is null or not found.");
            }

            _IsSpecial = isSpecial;
            _IconSize  = iconSize;

            // Dim mm As New MemPtr
            // Dim res As HResult = SHCreateItemFromParsingName(path, Nothing, Guid.Parse(ShellIIDGuid.IShellItem), shitem)
            // Dim fp As String = Nothing
            // Dim parts As String() = Nothing

            // If (res = HResult.Ok) Then
            // shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, mm)

            // fp = mm
            // ParsingName = fp

            // mm.CoTaskMemFree()
            // ElseIf (path.Substring(0, 2) = "::") Then
            // parts = path.Split("\")

            // res = SHCreateItemFromParsingName(parts(parts.Length - 1), Nothing, Guid.Parse(ShellIIDGuid.IShellItem), shitem)
            // If res = HResult.Ok Then
            // shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, mm)

            // fp = mm
            // ParsingName = fp

            // mm.CoTaskMemFree()
            // End If
            // End If

            // res = SHCreateItemFromParsingName("shell:" + If(parts IsNot Nothing, parts(parts.Length - 1), If(fp, path)), Nothing, Guid.Parse(ShellIIDGuid.IShellItem), shitem)

            // If (res = HResult.Ok) Then
            // Dim ip As IntPtr

            // res = shitem.GetDisplayName(ShellItemDesignNameOptions.DesktopAbsoluteParsing, ip)
            // mm = ip

            // If (res = HResult.Ok) Then
            // CanonicalName = If(fp Is Nothing, "shell:" + path, mm.ToString())
            // If (ParsingName Is Nothing) Then ParsingName = mm

            // _Path = ParsingName

            // mm.CoTaskMemFree()
            // shitem.GetDisplayName(ShellItemDesignNameOptions.Normal, mm)

            // DisplayName = mm
            // mm.CoTaskMemFree()
            // End If
            // End If


            // shitem = Nothing

            if (_IsSpecial)
            {
                // let's see if we can parse it.
                IShellItem shitem  = null;
                var        mm      = new MemPtr();
                var        argriid = Guid.Parse(ShellIIDGuid.IShellItem);
                var        res     = NativeShell.SHCreateItemFromParsingName(parsingName, IntPtr.Zero, ref argriid, ref shitem);
                string     fp      = null;
                if (res == HResult.Ok)
                {
                    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();

                    _IsSpecial = true;

                    if (initialize)
                    {
                        Refresh(_IconSize);
                    }
                    else
                    {
                        _Folders.Add(new DirectoryObject());
                        OnPropertyChanged(nameof(Folders));
                    }

                    return;
                }

                HResult localSHCreateItemFromParsingName()
                {
                    var argriid = Guid.Parse(ShellIIDGuid.IShellItem); var ret = NativeShell.SHCreateItemFromParsingName("shell:" + (fp ?? parsingName), IntPtr.Zero, ref argriid, 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;
                    }
                    _Path = ParsingName;
                    mm.CoTaskMemFree();

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

                    DisplayName = (string)mm;
                    mm.CoTaskMemFree();
                }

                shitem = null;
                if (!string.IsNullOrEmpty(DisplayName) && !string.IsNullOrEmpty(parsingName))
                {
                    _IsSpecial = true;
                    if (initialize)
                    {
                        Refresh(_IconSize);
                    }
                    else
                    {
                        _Folders.Add(new DirectoryObject());
                        OnPropertyChanged(nameof(Folders));
                    }

                    return;
                }
            }

            if (!string.IsNullOrEmpty(ParsingName))
            {
                _Path = ParsingName;
            }
            else
            {
                _Path       = parsingName;
                ParsingName = parsingName;
            }

            if (System.IO.Directory.Exists(_Path) == false)
            {
                return;
            }
            // Throw New DirectoryNotFoundException("Directory Not Found: " & _Path)
            else
            {
                DisplayName   = Path.GetFileName(_Path);
                CanonicalName = _Path;
                parsingName   = _Path;
            }

            if (initialize)
            {
                Refresh(_IconSize);
            }
            else
            {
                _Folders.Add(new DirectoryObject());
                OnPropertyChanged(nameof(Folders));
            }
        }
Exemplo n.º 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
            {
            }
        }