本文整理汇总了C#中SHFILEINFO类的典型用法代码示例。如果您正苦于以下问题:C# SHFILEINFO类的具体用法?C# SHFILEINFO怎么用?C# SHFILEINFO使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SHFILEINFO类属于命名空间,在下文中一共展示了SHFILEINFO类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSmallFileIcon
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();
}
示例2: GetFileIcon
/// <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;
}
示例3: GetExtensionIcon
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;
}
}
示例4: SHFILEINFO
/// <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];
}
}
示例5: GetIcon
/// <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);
}
}
}
示例6: SHGetFileInfo
private static extern int SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);
示例7: GetDirectoryIcon
/// <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;
}
示例8: GetShellFileInfo
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;
}
示例9: FormatType
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;
}
示例10: SHGetFileInfo
public static extern IntPtr SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbFileInfo,
uint uFlags
);
示例11: GetFolderIcon
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;
}
示例12: FileIconIndex
public static int FileIconIndex(string AFileName)
{
SHFILEINFO vFileInfo = new SHFILEINFO();
SHGetFileInfo(AFileName, 0, ref vFileInfo,
Marshal.SizeOf(vFileInfo), SHGFI_SYSICONINDEX);
return vFileInfo.iIcon;
}
示例13: ToImageSource
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;
}
示例14: GetIcon
/// <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;
}
示例15: ListViewSysImages
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);
}