상세 컨텐츠

본문 제목

파일과 연결된 아이콘 가져오기

C#

by 탑~! 2014. 3. 21. 09:27

본문

SHGetFileInfo 함수를 사용 하는 방법



using System.Runtime.InteropServices;


[StructLayout(LayoutKind.Sequential)] 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; }; class Win32 { public const uint SHGFI_ICON = 0x100; public const uint SHGFI_LARGEICON = 0x0; // 'Large icon public const uint SHGFI_SMALLICON = 0x1; // 'Small icon [DllImport("shell32.dll")] public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); }


private int nIndex = 0;


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 = 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);
				
    //Add file name and icon to listview
    listView1.Items.Add(fName, nIndex++); 
}



출처 : http://support.microsoft.com/kb/319350/ko



FileIcon_Src.zip


OSIcon_src_v2.0.zip


'C#' 카테고리의 다른 글

한자 등록시 ? 표시 해결하기  (0) 2014.03.28
c# autoscroll  (0) 2014.03.28
웹 이미지 다운로드  (0) 2014.03.05
외부 응용프로그램 실행하기  (0) 2014.03.05
직접 작성한 어셈블리를 Visual Studio .NET 탭에 등록  (0) 2014.01.03

관련글 더보기