2014-06-17 2 views
0

게시되지 않았지만 azure 저장소를 사용중인 경우 파일 추출 아이콘이 작동합니다.Azure 검색 파일 아이콘이 작동하지 않습니다. SHGetFileInfo

프로젝트를 게시 할 때 (여전히 같은 저장소, 하늘색 웹 사이트를 무료/공유 함) 파일 아이콘 (500 내부 서버 오류)을 추출 할 수 없습니다.

올리기 ICON

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; 
      } 
     } 
    } 

ERRORMESSAGE 브라우저 콘솔 창에서 :

POST mywebsite.azurewebsites.net/Folder/ 500 (내부 서버 E) 업로드 오류)

어떻게 작동하지 않습니까?

정말 감사드립니다. 감사합니다.

+0

100 % 확실하지만, 오류가 (당신이 예를 들어, 레지스트리를 조작 할 수 없습니다) 꽤 잠겨 모드에서 실행 하늘빛 웹 사이트로 푸른 웹 사이트에 많은 웹 사이트 특정 코드를 실행하는 권한의 부족과 관련이있을 수 있습니다 단일 VM에서 실행됩니다. –

+0

그래서 기본적으로 나는 망했다; D? – Reft

+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); 

    } 
} 
+0

이것은 사실 너무 좋기 때문에, Omg은 당신에게 아주 친절합니다. 약 950 개의 아이콘을 발견했습니다. 모두 다른 이름입니까? 내가 모두 같은 용기에 넣으면 합병증이 생길까요? 감사합니다. 감사합니다. – Reft

+0

나는 당신을 도울 수있어서 기뻤습니다. 다른 이름에 관해서는 각 아이콘이 컴퓨터의 다른 등록 파일 유형을위한 것이라고 예로 말합니다. 이 아이콘을 하나의 컨테이너에 넣는 것만 큼 복잡하지 않아야합니다. 내가 한 일은 아이콘의 품질이 그리 좋지 않다는 것입니다. 하지만 네가 그걸로 살 수 있다면, 모두 괜찮을거야. –

+0

나는 그걸로 괜찮습니다 :) 한 laaaast 질문, (약속)! 그 목록에있는 각 파일의 파일 확장명을 원하면 ..? Foreach 파일은 001, 002 등의 이름을 부여합니다 (?), 왜 그런가요? – Reft

관련 문제