Beispiel #1
0
        public static string CustomFileIcon(string fileName)
        {
            if (string.IsNullOrEmpty(fileName) || (appImages == null))
            {
                return(null);
            }

            string imageName = null;

            if (FileOperations.FileIsLink(fileName))
            {
                string tempName = FileOperations.ExtractFileNameFromShellLink(fileName);
                if (!string.IsNullOrEmpty(tempName))
                {
                    fileName = tempName;
                }
            }

            string shortName = Path.GetFileName(fileName);

            imageName = appImages.ReadString("Program", shortName);
            if (!string.IsNullOrEmpty(imageName))
            {
                if (FileOperations.FileExists(imageName))
                {
                    return(imageName);
                }
            }
            return(null);
        }
Beispiel #2
0
        private static Image FileNameImageInternal(string fileName)
        {
            Image result = null;
            Icon  icon   = null;

            if (!string.IsNullOrEmpty(fileName))
            {
                fileName = FileOperations.StripFileName(fileName);
                //if we have only name of the file without path
                //try to find it in the search path of Windows
                fileName = FileSearch.FullPath(fileName);

                if (!FileOperations.FileOrFolderExists(fileName))
                {
                    result = FileTypeImage(fileName);
                    return(result);
                }

                //if this is a link to executable with original
                //executable icon
                if (FileOperations.FileIsLink(fileName))
                {
                    result = TryExtractImageFromShellLink(fileName);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                if (FileOperations.FileIsImage(fileName))
                {
                    try
                    {
                        result = Image.FromFile(fileName);
                        result = BitmapPainter.ResizeBitmap(result, imageSize, imageSize, true);
                    }
                    catch
                    {
                        result = null;
                    }
                    if (result != null)
                    {
                        return(result);
                    }
                }

                // 1. The file is icon
                // 2. The file is exe or dll
                // 3. The file is a shell link .lnk
                // 4. Other file types
                if (FileOperations.FileIsIcon(fileName))
                {
                    //Simply load icon from file
                    icon = new Icon(fileName);
                }
                else
                //Extract icon from exe or dll directly
                if (FileOperations.FileIsExe(fileName))
                {
                    return(ExtractIconFromExecutable(fileName));
                }
                else
                {
                    result = FileTypeImage(fileName);
                    return(result);
                }

                result = ConvertIconToBitmap(icon);
            }

            if (icon != null)
            {
                icon.Dispose();
            }

            return(result);
        }