如何从网络共享文件中获取关联的图标

时间:2009-12-03 19:15:04

标签: .net winforms

我正在使用Icon.ExtractAssociatedIcon在openfiledialog中获取用户选择的文件图标。

问题是如果用户从网络共享中选择一个图标,那么openfiledialog的filename属性是UNC格式,这会导致ArgumentException中的ExtractAssocaitedIcon

Value of '\\server\share\filename' is not valid for 'filePath'.

Stack Trace:
   at System.Drawing.Icon.ExtractAssociatedIcon(String filePath, Int32 index)

所以我的问题是指定为\\server\share\filename的文件,如何获取图标?

注意: .NET 2.0

5 个答案:

答案 0 :(得分:29)

使用Reflector查看此内容时,最终会在ExtractAssociatedIcon中调用shell32.dll

您是否尝试过通过PInvoke访问BCL?

示例代码(通过PInvoke.Net):

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, StringBuilder lpIconPath,
   out ushort lpiIcon);

 // ... snip
    ushort uicon;
    StringBuilder strB = new StringBuilder(260); // Allocate MAX_PATH chars
    strB.Append(openFileDialog1.FileName);
    IntPtr handle = ExtractAssociatedIcon(IntPtr.Zero, strB, out uicon);
    Icon ico = Icon.FromHandle(handle);

    pictureBox1.Image = ico.ToBitmap();
 // ... snip

答案 1 :(得分:20)

为了完整起见,这是一个有效的ExtractAssociatedIcon例程:

/// <summary>
/// Returns an icon representation of an image contained in the specified file.
/// This function is identical to System.Drawing.Icon.ExtractAssociatedIcon, xcept this version works.
/// </summary>
/// <param name="filePath">The path to the file that contains an image.</param>
/// <returns>The System.Drawing.Icon representation of the image contained in the specified file.</returns>
/// <exception cref="System.ArgumentException">filePath does not indicate a valid file.</exception>
public static Icon  ExtractAssociatedIcon(String filePath)
{
    int index = 0;

    Uri uri;
    if (filePath == null)
    {
        throw new ArgumentException(String.Format("'{0}' is not valid for '{1}'", "null", "filePath"), "filePath");
    }
    try
    {
        uri = new Uri(filePath);
    }
    catch (UriFormatException)
    {
        filePath = Path.GetFullPath(filePath);
        uri = new Uri(filePath);
    }
    //if (uri.IsUnc)
    //{
    //  throw new ArgumentException(String.Format("'{0}' is not valid for '{1}'", filePath, "filePath"), "filePath");
    //}
    if (uri.IsFile)
    {
        if (!File.Exists(filePath))
        {
            //IntSecurity.DemandReadFileIO(filePath);
            throw new FileNotFoundException(filePath);
        }

        StringBuilder iconPath = new StringBuilder(260);
        iconPath.Append(filePath);

        IntPtr handle = SafeNativeMethods.ExtractAssociatedIcon(new HandleRef(null, IntPtr.Zero), iconPath, ref index);
        if (handle != IntPtr.Zero)
        {
            //IntSecurity.ObjectFromWin32Handle.Demand();
            return Icon.FromHandle(handle);
        }
    }
    return null;
}


/// <summary>
/// This class suppresses stack walks for unmanaged code permission. 
/// (System.Security.SuppressUnmanagedCodeSecurityAttribute is applied to this class.) 
/// This class is for methods that are safe for anyone to call. 
/// Callers of these methods are not required to perform a full security review to make sure that the 
/// usage is secure because the methods are harmless for any caller.
/// </summary>
[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
    [DllImport("shell32.dll", EntryPoint = "ExtractAssociatedIcon", CharSet = CharSet.Auto)]
    internal static extern IntPtr ExtractAssociatedIcon(HandleRef hInst, StringBuilder iconPath, ref int index);
}
  

注意:任何代码都会发布到公共域中。无需归属。

答案 2 :(得分:2)

实现此目的的一种方法是检索您的UNC路径并暂时将其映射到驱动器号,然后在.ExtractAssociatedIcon方法中使用该驱动器。检索到图标后,可以取消映射驱动器。 它不优雅,但应该可以正常工作。

答案 3 :(得分:1)

另一种选择是将用户选择的文件复制到%TEMP%并在那里使用Icon.ExtractAssociatedIcon。记得自己清理一下。

如果你支持LARGE文件,显然不是一个好的解决方案!

答案 4 :(得分:0)

仅是对Ian Boyd答案的补充,如果文件是图像而不是文件图标,则可以使用FileStream对其进行预览:

foreach (string item in Directory.GetFiles(actualPath))
{

    fi = new FileInfo(item);

    using (FileStream stream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
     {
          myImageList.Images.Add(Image.FromStream(stream));

     }
}