private static string FormatType(FileInfo fileInfo) { var info = new SHFILEINFO(); const uint flags = (uint) SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES; SHGetFileInfo(fileInfo.FullName, FILE_ATTRIBUTE_NORMAL, ref info, (uint) Marshal.SizeOf(info), flags); return info.szTypeName; }
public static extern IntPtr SHGetFileInfo( string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags );
/// <summary> /// Get the associated Icon for a file or application, this method always returns /// an icon. If the strPath is invalid or there is no idonc the defaulticon is returned /// </summary> /// <param name="strPath">full path to the file or directory</param> /// <param name="bSmall">if true, the 16x16 icon is returned otherwise the32x32</param> /// <param name="bOpen">if true, and strPath is a folder, returns the 'open' icon rather than the 'closed'</param> /// <returns></returns> public static Icon GetIcon(string strPath, bool bSmall, bool bOpen) { // Detect Operating System // From: http://www.mono-project.com/FAQ:_Technical int p = (int)Environment.OSVersion.Platform; if ((p == 4) || (p == 6) || (p == 128)) { return GetIconFromResources(strPath); } else { try { SHFILEINFO info = new SHFILEINFO(true); int cbFileInfo = Marshal.SizeOf(info); SHGFI flags; if (bSmall) flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes; else flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes; if (bOpen) flags = flags | SHGFI.OpenIcon; SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags); return Icon.FromHandle(info.hIcon); } catch { return GetIconFromResources(strPath); } } }
public static Bitmap GetSmallFileIcon(string filename) { SHFILEINFO shinfo = new SHFILEINFO(); Win32.SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON); Icon i = Icon.FromHandle(shinfo.hIcon); return i.ToBitmap(); }
public static int FileIconIndex(string AFileName) { SHFILEINFO vFileInfo = new SHFILEINFO(); SHGetFileInfo(AFileName, 0, ref vFileInfo, Marshal.SizeOf(vFileInfo), SHGFI_SYSICONINDEX); return vFileInfo.iIcon; }
/// <summary> /// Get the icon for a directory. /// </summary> /// <param name="small">true to retrieve a small icon; false otherwise.</param> /// <param name="open">true to retrieve an icon depicting an open item; false otherwise.</param> /// <returns>The directory icon.</returns> public static Icon GetDirectoryIcon(bool small, bool open) { uint flags = SHGFI_ICON; if (small) { flags |= SHGFI_SMALLICON; } else { flags |= SHGFI_LARGEICON; } if (open) { flags |= SHGFI_OPENICON; } SHFILEINFO shellFileInfo = new SHFILEINFO(); IntPtr success = SHGetFileInfo(null, 0, ref shellFileInfo, (uint)Marshal.SizeOf(shellFileInfo), flags); // hack to ensure the icon handle is properly freed Icon tempIcon = Icon.FromHandle(shellFileInfo.hIcon); Icon icon = (Icon)tempIcon.Clone(); tempIcon.Dispose(); NativeMethods.DestroyIcon(shellFileInfo.hIcon); return icon; }
private static extern int SHGetFileInfo( string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbfileInfo, SHGFI uFlags );
public static Icon GetExtensionIcon(string aFileExtension, bool largeIcon) { string vFileName = String.Format("*.{0}", aFileExtension); SHFILEINFO shinfo = new SHFILEINFO(); IntPtr hImg; if (largeIcon) { hImg = SHGetFileInfo(vFileName, FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES); } else { hImg = SHGetFileInfo(vFileName, FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES); } try { return Icon.FromHandle(shinfo.hIcon); } catch { return null; } }
public static Icon GetFolderIcon() { // Need to add size check, although errors generated at present! uint flags = SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON; // Get the folder icon var shfi = new SHFILEINFO(); var res = SHGetFileInfo(@"C:\Windows", FILE_ATTRIBUTE_DIRECTORY, out shfi, (uint)Marshal.SizeOf(shfi), flags); if (res == IntPtr.Zero) throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); // Load the icon from an HICON handle Icon.FromHandle(shfi.hIcon); // Now clone the icon, so that it can be successfully stored in an ImageList var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone(); DestroyIcon(shfi.hIcon); // Cleanup return icon; }
/// <summary> /// Gets the icon asotiated with the filename. /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static Icon GetFileIcon(string fileName) { System.Drawing.Icon myIcon = null; try { IntPtr hImgSmall; //the handle to the system image list SHFILEINFO shinfo = new SHFILEINFO(); //Use this to get the small Icon hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | (Win32.SHGFI_LARGEICON | Win32.SHGFI_USEFILEATTRIBUTES)); //The icon is returned in the hIcon member of the shinfo //struct myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon); } catch { return null; } return myIcon; }
private static SHFILEINFO GetShellFileInfo(string path, uint fileAttributes) { var info = new SHFILEINFO(); const uint flags = SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_SMALLICON; SHGetFileInfo(path, fileAttributes, ref info, (uint) Marshal.SizeOf(info), flags); return info; }
/// <summary> /// Returns an icon that should be utilised to represent the specified path /// within a directory listing. /// </summary> /// <param name="path">The path (directory or file name) to fetch an icon for</param> /// <returns>The index into the image list for the icon associated with the path</returns> public int this[string path] { get { // Determine the index of the icon in the system image list SHFILEINFO shinfo = new SHFILEINFO(); IntPtr hImageList = SHGetFileInfo(path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_SYSICONINDEX | SHGFI_SMALLICON); // If we haven't fetched this icon yet if (!shellIndexToOurIndex.ContainsKey(shinfo.iIcon)) { // Fetch the icon IntPtr hIcon = ImageList_GetIcon(hImageList, shinfo.iIcon, ILD_NORMAL); Icon myIcon = Icon.FromHandle(hIcon); DestroyIcon(hIcon); // And add it to our managed image list shellIndexToOurIndex.Add(shinfo.iIcon, imageList.Images.Count); imageList.Images.Add(myIcon); } // Return the index of the icon to use for this path. return shellIndexToOurIndex[shinfo.iIcon]; } }
public static void ListViewSysImages(ListView AListView) { SHFILEINFO vFileInfo = new SHFILEINFO(); IntPtr vImageList = SHGetFileInfo("", 0, ref vFileInfo, Marshal.SizeOf(vFileInfo), SHGFI_SHELLICONSIZE | SHGFI_SYSICONINDEX | SHGFI_LARGEICON); SendMessage(AListView.Handle, LVM_SETIMAGELIST, (IntPtr)LVSIL_NORMAL, vImageList); vImageList = SHGetFileInfo("", 0, ref vFileInfo, Marshal.SizeOf(vFileInfo), SHGFI_SHELLICONSIZE | SHGFI_SYSICONINDEX | SHGFI_SMALLICON); SendMessage(AListView.Handle, LVM_SETIMAGELIST, (IntPtr)LVSIL_SMALL, vImageList); }
/// <summary> /// 获得文件夹图标 /// </summary> /// <returns></returns> public static Icon GetDirectoryIcon() { SHFILEINFO _SHFILEINFO = new SHFILEINFO(); IntPtr _IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON)); if (_IconIntPtr.Equals(IntPtr.Zero)) return null; Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon); return _Icon; }
public static ImageSource ToImageSource(this string path) { SHFILEINFO shinfo = new SHFILEINFO(); Win32.SHGetFileInfo(path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON); Icon myIcon = Icon.FromHandle(shinfo.hIcon); ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(myIcon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); return imageSource; }
/// <summary> /// 获得文件图标 /// </summary> /// <param name="FullFileName"></param> /// <returns></returns> public static Icon GetFileIcon(string FullFileName) { SHFILEINFO _SHFILEINFO = new SHFILEINFO(); IntPtr _IconIntPtr = SHGetFileInfo(FullFileName, 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_USEFILEATTRIBUTES)); if (_IconIntPtr.Equals(IntPtr.Zero)) return null; Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon); return _Icon; }
/// <summary> /// 获取系统图标 /// </summary> /// <param name="path">文件名</param> /// <param name="dwAttr">文件信息</param> /// <param name="dwFlag">获取信息控制字</param> /// <returns></returns> private Icon GetIcon( string path, FILE_ATTRIBUTE dwAttr, SHGFI dwFlag) { SHFILEINFO fi = new SHFILEINFO(); Icon ic = null; int iTotal = (int)SHGetFileInfo(path, dwAttr, ref fi, 0, dwFlag); ic = Icon.FromHandle(fi.hIcon); return ic; }
/// <summary> /// ファイルの種類取得 /// </summary> /// <param name="name"></param> /// <returns></returns> public static string GetTypeName(string name) { SHFILEINFO shfi = new SHFILEINFO(); shfi.szDisplayName = new String(Convert.ToChar(0), 260); shfi.szTypeName = new String(Convert.ToChar(0), 80); IntPtr hSuccess = SHGetFileInfo(name, 0, ref shfi, (uint)Marshal.SizeOf(shfi), SHGFI_TYPENAME); return shfi.szTypeName; }
public static Icon GetSmallIcon(string fileName) { SHFILEINFO shinfo = new SHFILEINFO(); Win32.SHGetFileInfo(fileName, 0, ref shinfo,(uint)Marshal.SizeOf(shinfo),Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON); if (shinfo.hIcon.ToInt32() == 0) return null; Icon shellIcon = (Icon)System.Drawing.Icon.FromHandle(shinfo.hIcon).Clone(); Win32.DestroyIcon(shinfo.hIcon); return shellIcon; }
public static Icon GetLargeIcon(string fileName) { IntPtr hImgLarge; //the handle to the system image list SHFILEINFO shinfo = new SHFILEINFO(); hImgLarge = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON | Win32.USEFILEATTRIBUTES); Icon icon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone(); Win32.DestroyIcon(shinfo.hIcon); return icon; }
public static Icon GetSmallTypeIcon(string ext) { SHFILEINFO shinfo = new SHFILEINFO(); Win32.SHGetFileInfo(ext, Win32.FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON | Win32.SHGFI_USEFILEATTRIBUTES); if (shinfo.hIcon.ToInt32() == 0) return null; Icon shellIcon = (Icon)System.Drawing.Icon.FromHandle(shinfo.hIcon).Clone(); Win32.DestroyIcon(shinfo.hIcon); return shellIcon; }
private const uint SHGFI_SMALLICON = 0x1; // 'Small icon #endregion Fields #region Methods public static string DisplayName(string fileName) { if(MonoHelper.IsUnix) return null; SHFILEINFO shinfo = new SHFILEINFO(); NativeMethods.SHGetFileInfo(fileName, 0, ref shinfo,(uint)Marshal.SizeOf(shinfo),SHGFI_DISPLAYNAME); return shinfo.szDisplayName; }
public static string GetDisplayName(string strPath) { SHFILEINFO info = new SHFILEINFO(true); int cbFileInfo = Marshal.SizeOf(info); SHGFI flags; flags = SHGFI.DisplayName; SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags); return info.szDisplayName; }
/// <summary> /// Get the associated Icon for a file or application, this method always returns /// an icon. If the strPath is invalid or there is no idonc the default icon is returned /// </summary> /// <param name="strPath">full path to the file</param> /// <param name="bSmall">if true, the 16x16 icon is returned otherwise the 32x32</param> public static Icon GetIcon(string strPath, bool bSmall) { SHFILEINFO info = new SHFILEINFO(true); int cbFileInfo = Marshal.SizeOf(info); SHGFI flags; if (bSmall) flags = SHGFI.Icon|SHGFI.SmallIcon|SHGFI.UseFileAttributes; else flags = SHGFI.Icon|SHGFI.LargeIcon|SHGFI.UseFileAttributes; SHGetFileInfo(strPath, 256, out info,(uint)cbFileInfo, flags); return Icon.FromHandle(info.hIcon); }
private static Icon GetIcon(string fileName, uint flags) { SHFILEINFO shinfo = new SHFILEINFO(); IntPtr hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | flags); Icon icon = (Icon)System.Drawing.Icon.FromHandle(shinfo.hIcon).Clone(); Win32.DestroyIcon(shinfo.hIcon); return icon; }
private static BitmapSource getIcon(string fileName, uint flags) { //Use this to get the small Icon var shinfo = new SHFILEINFO(); Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | flags); if (shinfo.hIcon == IntPtr.Zero) return null; return Imaging.CreateBitmapSourceFromHIcon(shinfo.hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); }
public static string GetTypeName(string strPath) { SHFILEINFO fileInfo = new SHFILEINFO(); int cbFileInfo = Marshal.SizeOf(fileInfo); SHGetFileInfo(strPath, (uint)FileAttributeFlags.FILE_ATTRIBUTE_NORMAL, ref fileInfo, (uint)cbFileInfo, (uint)(GetFileInfoFlags.SHGFI_DISPLAYNAME | GetFileInfoFlags.SHGFI_TYPENAME)); return fileInfo.szTypeName; }
public static Icon GetSmallIcon(string fileName) { IntPtr hImgSmall; //the handle to the system image list SHFILEINFO shinfo = new SHFILEINFO(); hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON | Win32.USEFILEATTRIBUTES); //The icon is returned in the hIcon member of the shinfo struct Icon icon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone(); Win32.DestroyIcon(shinfo.hIcon); return icon; }
public static Icon GetSmallIcon(string fileName) { IntPtr hImgSmall; //the handle to the system image list SHFILEINFO shinfo = new SHFILEINFO(); //Use this to get the small Icon hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON); //The icon is returned in the hIcon member of the shinfo struct return System.Drawing.Icon.FromHandle(shinfo.hIcon); }
public static SHFILEINFO GetFileInfo(string path, int flag) { StringBuilder iconPath = new StringBuilder(260); iconPath.Append(path); SHFILEINFO fileInfo = new SHFILEINFO(); int size = Marshal.SizeOf(fileInfo); SHGetFileInfo(iconPath, 0, ref fileInfo, size, flag); return fileInfo; }
public static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttribs, out SHFILEINFO psfi, uint cbFileInfo, SHGFI uFlags);
public static extern IntPtr SHGetFileInfo(IntPtr pIDL, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, SHGFI uFlags);
public static extern IntPtr SHGetFileInfo(String pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
private static extern int SHGetFileInfo( string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags);
internal static extern IntPtr SHGetFileInfo( [MarshalAs(UnmanagedType.LPWStr)] string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);