示例#1
0
        public Image GetIcon(string aPath, FileIconSize aSize)
        {
            var info  = new SafeNativeMethods.SHFILEINFO();
            var sInfo = Marshal.SizeOf(info);

            var flags = SafeNativeMethods.SHGFI.Icon | SafeNativeMethods.SHGFI.SysIconIndex;

            if (SafeNativeMethods.SHGetFileInfo(aPath, 0, out info, (uint)sInfo, flags) == 0)
            {
                flags = SafeNativeMethods.SHGFI.Icon | SafeNativeMethods.SHGFI.SmallIcon | SafeNativeMethods.SHGFI.UseFileAttributes;
                if (SafeNativeMethods.SHGetFileInfo(aPath, 0x80, out info, (uint)sInfo, flags) == 0)
                {
                    throw new ArgumentException("Cannot get the Icon Index");
                }
            }
            SafeNativeMethods.DestroyIcon(info.hIcon);

            var hIcon = SafeNativeMethods.ImageList_GetIcon(ImageLists[(int)aSize], info.iIcon, 0);

            if (hIcon == IntPtr.Zero)
            {
                throw new ArgumentException("Cannot get the Icon");
            }

            var bmp = Icon.FromHandle(hIcon).ToBitmap();

            SafeNativeMethods.DestroyIcon(hIcon);
            return(bmp);
        }
        public static Image GetFileIcon(string fullpath, FileIconSize size)
        {
            UnsafeNativeMethods.SHFILEINFO info = new UnsafeNativeMethods.SHFILEINFO();

            uint flags = UnsafeNativeMethods.SHGFI_USEFILEATTRIBUTES | UnsafeNativeMethods.SHGFI_ICON;

            if (size == FileIconSize.Small)
            {
                flags |= UnsafeNativeMethods.SHGFI_SMALLICON;
            }

            int retval = UnsafeNativeMethods.SHGetFileInfo(fullpath, UnsafeNativeMethods.FILE_ATTRIBUTE_NORMAL,
                                                           ref info, System.Runtime.InteropServices.Marshal.SizeOf(info), flags);

            if (retval == 0)
            {
                return(null);  // error occured
            }

            System.Drawing.Icon icon    = System.Drawing.Icon.FromHandle(info.hIcon);
            ImageList           imglist = new ImageList();

            imglist.ImageSize = icon.Size;
            imglist.Images.Add(icon);
            Image image = imglist.Images[0];

            icon.Dispose();
            return(image);
        }
示例#3
0
        public static Image GetFileIconAsImage(string fullpath, FileIconSize size)
        {
            SHFILEINFO info = new SHFILEINFO();

            uint flags = SHGFI_USEFILEATTRIBUTES | SHGFI_ICON;

            if (size == FileIconSize.Small)
            {
                flags |= SHGFI_SMALLICON;
            }

            int retval = SHGetFileInfo(fullpath, FILE_ATTRIBUTE_NORMAL, ref info, System.Runtime.InteropServices.Marshal.SizeOf(info), flags);

            if (retval == 0)
            {
                return(null);    // error occured
            }

            System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(info.hIcon);

            //ImageList imglist = new ImageList();
            //imglist.ImageSize = icon.Size;
            //imglist.Images.Add(icon);

            //Image image = imglist.Images[0];
            //icon.Dispose();
            //return image;
            return(icon.ToBitmap());
        }
示例#4
0
        public static void UpdateSystemImageList(ImageList imageList, FileIconSize size, bool isSelected, Image deletedImage)
        {
            SHFILEINFO info  = new SHFILEINFO();
            uint       flags = SHGFI_SYSICONINDEX;

            if (size == FileIconSize.Small)
            {
                flags |= SHGFI_SMALLICON;
            }

            if (isSelected == true)
            {
                flags |= SHGFI_OPENICON;
            }

            int imageHandle = SHGetFileInfo("C:\\", 0, ref info, System.Runtime.InteropServices.Marshal.SizeOf(info), flags);
            int iconCount   = ImageList_GetImageCount(imageHandle);

            for (int i = imageList.Images.Count; i < iconCount; i++)
            {
                IntPtr iconHandle = (IntPtr)ImageList_GetIcon(imageHandle, i, 0);
                try {
                    if (iconHandle.ToInt64() != 0)
                    {
                        Icon icon = Icon.FromHandle(iconHandle);
                        imageList.Images.Add(icon);
                        icon.Dispose();
                        DestroyIcon(iconHandle);
                    }
                }
                catch {
                    imageList.Images.Add(deletedImage);
                }
            }
        }
示例#5
0
 public static Icon ExtractOne(string fileName, int index, FileIconSize size)
 {
     try {
         List <Icon> iconList = ExtractEx(fileName, size, index, 1);
         return(iconList[0]);
     } catch (UnableToExtractIconsException) {
         throw new IconNotFoundException(fileName, index);
     }
 }
示例#6
0
 public static Image GetIcon(string path, FileIconSize size)
 {
     if (impl != null) {
     try {
       return impl.GetIcon(path, size);
     }
     catch (Exception) {
     }
       }
       return null;
 }
示例#7
0
 public static Image GetIcon(string path, FileIconSize size)
 {
     if (impl != null)
     {
         try {
             return(impl.GetIcon(path, size));
         }
         catch (Exception) {
         }
     }
     return(null);
 }
示例#8
0
        // TOP LEVEL API

        public static MemoryStream GetFileIcon(string name, FileIconSize size)
        {
            Icon icon = FileIconUtils.IconFromExtension(name.GetAfter("."), size);

            using (icon) {
                using (var bmp = icon.ToBitmap()) {
                    MemoryStream ms = new MemoryStream();
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    ms.Seek(0, SeekOrigin.Begin);
                    return(ms);
                }
            }
        }
示例#9
0
        public static int GetFolderIconIndex(string fullpath, FileIconSize size)
        {
            SHFILEINFO info = new SHFILEINFO();

            uint flags = SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX;

            if (size == FileIconSize.Small)
            {
                flags |= SHGFI_SMALLICON;
            }

            int retval = SHGetFileInfo(fullpath, FILE_ATTRIBUTE_DIRECTORY, ref info, System.Runtime.InteropServices.Marshal.SizeOf(info), flags);

            if (retval == 0)
            {
                return(-1);      // error
            }

            return(info.iIcon);
        }
示例#10
0
        public static List <Icon> ExtractEx(string fileName, FileIconSize size,
                                            int firstIconIndex, int iconCount)
        {
            List <Icon> iconList = new List <Icon>();

            switch (size)
            {
            case FileIconSize.Large:
                ExtractEx(fileName, iconList, null, firstIconIndex, iconCount);
                break;

            case FileIconSize.Small:
                ExtractEx(fileName, null, iconList, firstIconIndex, iconCount);
                break;

            default:
                throw new ArgumentOutOfRangeException("size");
            }

            return(iconList);
        }
示例#11
0
        public static Icon GetFileIcon(string fullpath, FileIconSize size)
        {
            SHFILEINFO info = new SHFILEINFO();

            uint flags = SHGFI_USEFILEATTRIBUTES | SHGFI_ICON;

            if (size == FileIconSize.Small)
            {
                flags |= SHGFI_SMALLICON;
            }

            int retval = SHGetFileInfo(fullpath, FILE_ATTRIBUTE_NORMAL, ref info, System.Runtime.InteropServices.Marshal.SizeOf(info), flags);

            if (retval == 0)
            {
                return(null);    // error occured
            }

            System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(info.hIcon);
            return(icon);
        }
示例#12
0
        ///// <summary>
        ///// 删除文件,如果文件还被其他程序引用,将不会真正从文件系统删除
        ///// </summary>
        //public static void DeleteFile(string fileID)
        //{
        //    string[] fileIDs = new string[] { fileID };
        //    DeleteFiles(fileIDs);
        //}

        ///// <summary>
        ///// 删除文件,如果文件还被其他程序引用,将不会真正从文件系统删除
        ///// </summary>
        ///// <param name="fileID"></param>
        //public static void DeleteFiles(IEnumerable<string> fileIDs)
        //{
        //    DeleteFiles(fileIDs, null);
        //}

        //public delegate void DeleteFileCallback(PhysicalFileCollection deleteFiles);

        //public static void DeleteFiles(IEnumerable<string> fileIDs, DeleteFileCallback callback)
        //{
        //    if (ValidateUtil.HasItems<string>(fileIDs) == false)
        //        return;


        //    List<string> deleteFileIds = new List<string>(fileIDs);

        //    foreach (FileActionBase action in s_UploadActions.Values)
        //    {
        //        IEnumerable<string> tempIDs = action.CreateInstance().GetAlsoUsedFileIds(deleteFileIds);

        //        foreach (string tempID in tempIDs)
        //        {
        //            deleteFileIds.Remove(tempID);
        //        }

        //        //如果要删的文件都仍在被使用,那就无需删除任何文件,直接返回
        //        if (deleteFileIds.Count == 0)
        //            return;
        //    }

        //    PhysicalFileCollection deleteFiles = FileDao.Instance.GetFiles(deleteFileIds);

        //    List<string> filePaths = FileDao.Instance.DeleteFiles(deleteFileIds);

        //    foreach (string filePath in filePaths)
        //    {
        //        string savePath = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Upload_File), filePath);

        //        if (File.Exists(savePath))
        //        {
        //            File.Delete(savePath);
        //        }
        //    }

        //    if (callback != null)
        //        callback(deleteFiles);
        //}

        #endregion

        #region 被否决的

        public static string GetExtensionsIcon(string fileName, FileIconSize iconSize)
        {
            string extendname = string.Empty;

            if (fileName.Contains("."))
            {
                extendname = fileName.Substring(fileName.LastIndexOf('.') + 1);
            }
            extendname = extendname.ToLower();
            Dictionary <FileIconSize, Dictionary <string, string> > fileIconList = GetExtensionsImages();

            if (fileIconList[iconSize].ContainsKey(extendname))
            {
                return(fileIconList[iconSize][extendname]);
            }
            else if (fileIconList[iconSize].ContainsKey("default"))
            {
                return(fileIconList[iconSize]["default"]);
            }
            else
            {
                return(string.Empty);
            }
        }
示例#13
0
        //this will look throw the registry
        //to find if the Extension have an icon.
        public static Icon IconFromExtension(string extension,
                                             FileIconSize size)
        {
            // Add the '.' to the extension if needed
            if (extension[0] != '.')
            {
                extension = '.' + extension;
            }
            extension = extension.ToLower();

            //opens the registry for the wanted key.
            RegistryKey Root         = Registry.ClassesRoot;
            RegistryKey ExtensionKey = Root.OpenSubKey(extension);

            ExtensionKey.GetValueNames();
            RegistryKey ApplicationKey =
                Root.OpenSubKey(ExtensionKey.GetValue("").ToString());

            //gets the name of the file that have the icon.
            string IconLocation =
                ApplicationKey.OpenSubKey("DefaultIcon").GetValue("").ToString();

            string[] IconPath = IconLocation.Split(',');

            if (IconPath[1] == null)
            {
                IconPath[1] = "0";
            }
            IntPtr[] Large = new IntPtr[1], Small = new IntPtr[1];

            //extracts the icon from the file.
            ExtractIconEx(IconPath[0],
                          Convert.ToInt16(IconPath[1]), Large, Small, 1);
            return(size == FileIconSize.Large ?
                   Icon.FromHandle(Large[0]) : Icon.FromHandle(Small[0]));
        }
示例#14
0
 public OpenFileBehavior(Type openFileBehaviorSourceType, bool showIcon = true, FileIconSize iconSize = FileIconSize.Small,
                         Image defaultImage = null, Image invalidPathImage = null, CompletionMode mode = CompletionMode.FilesAndDirectories,
                         string filter      = null, string initDir = "", string fileMask = "All files (*.*)|*.*")
     : base(openFileBehaviorSourceType, showIcon, iconSize, defaultImage, invalidPathImage, mode, filter)
 {
     _initDir  = initDir;
     _fileMask = fileMask;
 }
示例#15
0
        public static List <Icon> Extract(string fileName, FileIconSize size)
        {
            int iconCount = GetIconsCountInFile(fileName);

            return(ExtractEx(fileName, size, 0, iconCount));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FileIconOptions"/> class.
 /// </summary>
 /// <param name="fileIconSize">Size of the file icon.</param>
 public FileIconOptions(FileIconSize fileIconSize)
 {
     Size = fileIconSize.ToString();
 }
示例#17
0
		/// <summary>
		/// Gets the icon associated with a given file.
		/// </summary>
		/// <param name="fullpath">Path to file.</param>
		/// <param name="iconSize">Size of icon to return.</param>
		/// <returns>Icon associated with the given file.</returns>
		public static Icon GetFileIcon(String fullpath, FileIconSize iconSize) 
		{
			var retval = 0;
			var info = new Win32API.Shell32.SHFILEINFO();
			uint flags = Win32API.Shell32.SHGFI_USEFILEATTRIBUTES | Win32API.Shell32.SHGFI_ICON;

			if (iconSize == FileIconSize.Small)
			{
				flags |= Win32API.Shell32.SHGFI_SMALLICON;
			} 
			else if (iconSize == FileIconSize.Large)
			{
				flags |= Win32API.Shell32.SHGFI_LARGEICON;
			}

			retval = Win32API.Shell32.SHGetFileInfo(fullpath, Win32API.Shell32.FILE_ATTRIBUTE_NORMAL, ref info, Marshal.SizeOf(info), flags);
			if (retval == 0) return null;

			return Icon.FromHandle(info.hIcon);
		}		
示例#18
0
        ///// <summary>
        ///// 删除文件,如果文件还被其他程序引用,将不会真正从文件系统删除
        ///// </summary>
        //public static void DeleteFile(string fileID)
        //{
        //    string[] fileIDs = new string[] { fileID };
        //    DeleteFiles(fileIDs);
        //}

        ///// <summary>
        ///// 删除文件,如果文件还被其他程序引用,将不会真正从文件系统删除
        ///// </summary>
        ///// <param name="fileID"></param>
        //public static void DeleteFiles(IEnumerable<string> fileIDs)
        //{
        //    DeleteFiles(fileIDs, null);
        //}

        //public delegate void DeleteFileCallback(PhysicalFileCollection deleteFiles);

        //public static void DeleteFiles(IEnumerable<string> fileIDs, DeleteFileCallback callback)
        //{
        //    if (ValidateUtil.HasItems<string>(fileIDs) == false)
        //        return;


        //    List<string> deleteFileIds = new List<string>(fileIDs);

        //    foreach (FileActionBase action in s_UploadActions.Values)
        //    {
        //        IEnumerable<string> tempIDs = action.CreateInstance().GetAlsoUsedFileIds(deleteFileIds);

        //        foreach (string tempID in tempIDs)
        //        {
        //            deleteFileIds.Remove(tempID);
        //        }

        //        //如果要删的文件都仍在被使用,那就无需删除任何文件,直接返回
        //        if (deleteFileIds.Count == 0)
        //            return;
        //    }

        //    PhysicalFileCollection deleteFiles = FileDao.Instance.GetFiles(deleteFileIds);

        //    List<string> filePaths = FileDao.Instance.DeleteFiles(deleteFileIds);

        //    foreach (string filePath in filePaths)
        //    {
        //        string savePath = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Upload_File), filePath);

        //        if (File.Exists(savePath))
        //        {
        //            File.Delete(savePath);
        //        }
        //    }

        //    if (callback != null)
        //        callback(deleteFiles);
        //}

        #endregion

        #region 被否决的

        public static string GetExtensionsIcon(string fileName, FileIconSize iconSize)
        {
            string extendname = string.Empty;
            if (fileName.Contains("."))
                extendname = fileName.Substring(fileName.LastIndexOf('.') + 1);
            extendname = extendname.ToLower();
            Dictionary<FileIconSize, Dictionary<string, string>> fileIconList = GetExtensionsImages();

            if (fileIconList[iconSize].ContainsKey(extendname))
                return fileIconList[iconSize][extendname];
            else if (fileIconList[iconSize].ContainsKey("default"))
                return fileIconList[iconSize]["default"];
            else
                return string.Empty;
        }
示例#19
0
 public static OpenFileBehavior Create(Type openFileBehaviorSourceType, bool showIcon = true, FileIconSize iconSize = FileIconSize.Small,
                                       Image defaultImage = null, Image invalidPathImage = null, CompletionMode mode = CompletionMode.FilesAndDirectories,
                                       string filter      = null, string initDir = "%TEMP%", string fileMask = "All files (*.*)|*.*")
 {
     return((OpenFileBehavior)Behavior.Create(typeof(OpenFileBehavior), openFileBehaviorSourceType, new object[8]
     {
         (object)showIcon,
         (object)iconSize,
         (object)defaultImage,
         (object)invalidPathImage,
         (object)mode,
         (object)filter,
         (object)initDir,
         (object)fileMask
     }));
 }