示例#1
0
 public static extern IntPtr SHGetFileInfo(IntPtr pidl,
                             uint dwFileAttributes,
                             ref SHFILEINFO psfi,
                             uint cbSizeFileInfo,
                             uint uFlags);
示例#2
0
        /// <summary>
        /// Sets the icon for the given path
        /// </summary>
        private void GenerateNodeDisplayDetails()
        {
            //if the path exists
            if (icon == null)
            {
                //needed to get a handle to our icon
                SHFILEINFO shinfo = new SHFILEINFO();

                //If we have an actual path, then we pass a string
                if (fullPath.Length > 0)
                {
                    //get the icon and display name
                    Win32.SHGetFileInfo(fullPath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON | Win32.SHGFI_DISPLAYNAME);
                }
                else
                {
                    //If we get a blank path we assume the root of our file system, so we get a pidl to My "Computer"

                    //Get a pidl to my computer
                    IntPtr tempPidl = System.IntPtr.Zero;
                    Win32.SHGetSpecialFolderLocation(0, Win32.CSIDL_DRIVES, ref tempPidl);

                    //get the icon and display name
                    Win32.SHGetFileInfo(tempPidl, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_PIDL | Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON | Win32.SHGFI_DISPLAYNAME);

                    //free our pidl
                    Marshal.FreeCoTaskMem(tempPidl);
                }

                //create the managed icon
                this.icon =(Icon)System.Drawing.Icon.FromHandle(shinfo.hIcon).Clone();
                this.szDisplayName = shinfo.szDisplayName;

                //dispose of the old icon
                Win32.DestroyIcon(shinfo.hIcon);
            }
        }