示例#1
0
        /// <summary>
        /// Gets the file icon currently in use by the operating system for the given file
        /// </summary>
        /// <param name="filepath">The full path to the file</param>
        /// <param name="smallIcon">If true the small icon is returned, otherwise the large icon is returned</param>
        /// <returns></returns>
        private static Icon GetFileIcon(string filepath, bool smallIcon)
        {
            SHFILEINFO shinfo = new SHFILEINFO();

            if (smallIcon)
            {
                SHGetFileInfo(filepath, 0, ref shinfo, (uint) Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);
            }
            else
            {
                SHGetFileInfo(filepath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);
            }

            return Icon.FromHandle(shinfo.hIcon);
        }
示例#2
0
        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr hImgSmall; //the handle to the system image list
            IntPtr hImgLarge; //the handle to the system image list
            string fName; //  'the file name to get icon from
            SHFILEINFO shinfo = new SHFILEINFO();

            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\temp\\";
            openFileDialog1.Filter = "All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            listView1.SmallImageList = imageList1;
            listView1.LargeImageList = imageList1;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fName = openFileDialog1.FileName;
                //Use this to get the small Icon
                //hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);

                //Use this to get the large Icon
                //hImgLarge = Win32.SHGetFileInfo(fName, 0,
                //    ref shinfo, (uint)Marshal.SizeOf(shinfo),
                //    Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);

                //The icon is returned in the hIcon member of the shinfo struct
                //System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);

                //imageList1.Images.Add(myIcon);
                imageList1.Images.Add(Win32.GetFileLargeIcon(fName));
                //imageList1.Images.Add(Win32.GetFileSmallIcon(fName));

                //Add file name and icon to listview
                listView1.Items.Add(fName, nIndex++);
            }
        }
示例#3
0
 public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);