Azure Retrieve文件图标无法发布,SHGetFileInfo

时间:2014-06-17 15:37:56

标签: azure

如果未发布,但使用azure storage live,则提取文件图标可以正常工作。

当我发布我的项目时,(仍然使用相同的存储,azure网站免费/共享),我无法提取文件图标,(500内部服务器错误)。

上传图标

Icon fileIcon = FileIconLoader.GetFileIcon(fileextension); 

CloudBlobContainer blobIconContainer = CloudStorageServices.GetCloudBlobIconContainer();
CloudBlockBlob blockBlob = blobIconContainer.GetBlockBlobReference(blobname);  

Bitmap pngIcon = fileIcon.ToBitmap();

MemoryStream ms = new MemoryStream(); 
pngIcon.Save(ms, ImageFormat.Png);
ms.Position = 0;
blockBlob.UploadFromStream(ms);

//检索图标功能

public static class FileIconLoader
    {
        private const uint SHGFI_ICON = 0x100;
        private const uint SHGFI_LARGEICON = 0x0;
        private const uint SHGFI_SMALLICON = 0x1;
        private const uint SHGFI_USEFILEATTRIBUTES = 0x10;

        private const uint FILE_ATTRIBUTE_NORMAL = 0x80;

        [DllImport("shell32.dll")]
        private static extern IntPtr SHGetFileInfo(string pszPath,
        uint dwFileAttributes,
        ref SHFILEINFO psfi,
        uint cbSizeFileInfo,
        uint uFlags);

        public static Icon GetFileIcon(string fileExtension)
        {
            fileExtension = "*" + fileExtension; //om inte fil finns 

            SHFILEINFO shinfo = new SHFILEINFO();
            IntPtr hImg;
            hImg = SHGetFileInfo(fileExtension, FILE_ATTRIBUTE_NORMAL, ref shinfo,
            (uint)Marshal.SizeOf(shinfo),
            SHGFI_ICON |
            SHGFI_LARGEICON |
            SHGFI_USEFILEATTRIBUTES);

            try
            {
                return Icon.FromHandle(shinfo.hIcon);
            }
            catch
            {
                return null;
            }
        }
    }

浏览器控制台窗口中的错误消息:

POST mywebsite.azurewebsites.net/Folder/Upload 500(内部服务器错误)

为什么它没有发布?

非常感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

这段代码是我们为此目的写回来的。它的作用是从注册表中找到所有已注册的文件类型(GetFileType方法),然后提取所有这些文件的图标并将其保存为PNG格式(GetIconImageFromFilename)。希望这会有所帮助。

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace RegistryFileExtensionWithIcon
{
    public class RegistryFileName
    {
        public static Dictionary<string, Bitmap> FileIconAssociation = new Dictionary<string, Bitmap>();

        public static List<string> GetFileType()
        {

            List<string> allFiles = new List<string>();
            try
            {
                // Create a registry key object to represent the HKEY_CLASSES_ROOT registry section
                RegistryKey rkRoot = Registry.ClassesRoot;

                //Gets all sub keys' names.
                string[] keyNames = rkRoot.GetSubKeyNames();
                //Hashtable iconsInfo = new Hashtable();

                //Find the file icon.
                foreach (string keyName in keyNames)
                {
                    if (String.IsNullOrEmpty(keyName))
                        continue;
                    int indexOfPoint = keyName.IndexOf(".");

                    //If this key is not a file exttension(eg, .zip), skip it.
                    if (indexOfPoint != 0)
                        continue;

                    RegistryKey rkFileType = rkRoot.OpenSubKey(keyName);
                    if (rkFileType == null)
                        continue;
                    allFiles.Add(keyName);
                    rkFileType.Close();
                }
                rkRoot.Close();
                return allFiles;
            }

            catch (Exception exc)
            {
                throw exc;
            }
        }

        public static void GetIconImageFromFilename(List<string> FileNames)
        {
            Bitmap bmpImage = null;
            string FileExtension = string.Empty;
            foreach (var file in FileNames)
            {
                int IndexOfLastDot = file.LastIndexOf(".");
                if (IndexOfLastDot >= 0)
                {
                    FileExtension = file.Substring(IndexOfLastDot + 1).ToLower();
                }
                if (!FileIconAssociation.TryGetValue(FileExtension, out bmpImage))
                {
                    IntPtr sImgSmall;    
                    SHFILEINFO shinfoForSmallIcon = new SHFILEINFO();
                    sImgSmall = Win32.SHGetFileInfo(file, 0, ref shinfoForSmallIcon, (uint)Marshal.SizeOf(shinfoForSmallIcon),Win32.SHGFI_USEFILEATTRIBUTES | Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON );
                    System.Drawing.Icon smallIcon = (System.Drawing.Icon)(System.Drawing.Icon.FromHandle(shinfoForSmallIcon.hIcon).Clone());
                    Win32.DestroyIcon(shinfoForSmallIcon.hIcon);
                    System.Drawing.Bitmap bmp1 = smallIcon.ToBitmap();
                    string location = System.IO.Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath) + "\\DownloadIcons\\16x16\\";
                    Directory.CreateDirectory(location);
                    string completeFilePath1 = string.Format("{0}{1}.png", location, FileExtension);
                    bmp1.Save(completeFilePath1, ImageFormat.Png);



                    IntPtr hImgLarge;   

                    SHFILEINFO shinfoForLargeIcon = new SHFILEINFO();
                    hImgLarge = Win32.SHGetFileInfo(file, 0, ref shinfoForLargeIcon, (uint)Marshal.SizeOf(shinfoForLargeIcon),
                        Win32.SHGFI_USEFILEATTRIBUTES | Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON );
                    System.Drawing.Icon largeIcon = (System.Drawing.Icon)(System.Drawing.Icon.FromHandle(shinfoForLargeIcon.hIcon).Clone());
                    Win32.DestroyIcon(shinfoForLargeIcon.hIcon);
                    System.Drawing.Bitmap bmp2 = largeIcon.ToBitmap();
                    string filePath = System.IO.Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath) + "\\DownloadIcons\\32x32\\";
                    Directory.CreateDirectory(filePath);

                    string completeFilePath2 = string.Format("{0}{1}.png", filePath, FileExtension);
                    bmp2.Save(completeFilePath2, ImageFormat.Png);

                }
            }
        }

        public struct SHFILEINFO
        {
            public IntPtr hIcon;
            public IntPtr iIcon;
            public uint dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        };
    }

    public class Win32
    {
        public const uint SHGFI_ICON = 0x000000100;
        public const uint SHGFI_DISPLAYNAME = 0x000000200;
        public const uint SHGFI_TYPENAME = 0x000000400;
        public const uint SHGFI_ATTRIBUTES = 0x000000800;
        public const uint SHGFI_ICONLOCATION = 0x000001000;
        public const uint SHGFI_EXETYPE = 0x000002000;
        public const uint SHGFI_SYSICONINDEX = 0x000004000;
        public const uint SHGFI_LINKOVERLAY = 0x000000000;// 0x000008000;
        public const uint SHGFI_SELECTED = 0x000010000;
        public const uint SHGFI_ATTR_SPECIFIED = 0x000020000;
        public const uint SHGFI_LARGEICON = 0x000000000;
        public const uint SHGFI_SMALLICON = 0x000000001;
        public const uint SHGFI_OPENICON = 0x000000002;
        public const uint SHGFI_SHELLICONSIZE = 0x000000004;
        public const uint SHGFI_PIDL = 0x000000008;
        public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
        public const uint SHGFI_ADDOVERLAYS = 0x000000020;
        public const uint SHGFI_OVERLAYINDEX = 0x000000040;
        //  public const uint SHGFI_ICON = 0x100;
        //  public const uint SHGFI_LARGEICON = 0x0;    // 'Large icon
        //  public const uint SHGFI_SMALLICON = 0x1;    // 'Small icon
        public const uint ILD_TRANSPARENT = 0x1;

        public const int GWL_STYLE = (-16);

        public const UInt32 SWP_FRAMECHANGED = 0x0020;
        public const UInt32 SWP_NOSIZE = 0x0001;
        public const UInt32 SWP_NOMOVE = 0x0002;
        public const int WS_SYSMENU = 0x00080000;

        [DllImport("user32.dll", SetLastError = true)]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
        {
            if (IntPtr.Size == 8)
                return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);

            return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
        }

        [DllImport("shell32.dll")]
        public static extern IntPtr SHGetFileInfo(string pszPath,
         uint dwFileAttributes,
         ref RegistryFileName.SHFILEINFO psfi,
         uint cbSizeFileInfo,
         uint uFlags);

        [DllImport("user32")]
        public static extern int DestroyIcon(IntPtr hIcon);
        [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
        private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
        private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        [DllImport("shell32.dll")]
        public static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);

    }
}