我正在使用Icon.ExtractAssociatedIcon来获取用户在openfiledialog中选择的文件的图标.
的问题是,如果用户选择从网络共享,则打开文件对话框的文件名属性是UNC格式,这将导致一个的图标ArgumentException
中ExtractAssocaitedIcon
:
Value of '\\server\share\filename' is not valid for 'filePath'.
Stack Trace:
at System.Drawing.Icon.ExtractAssociatedIcon(String filePath, Int32 index)
Run Code Online (Sandbox Code Playgroud)
所以我的问题是指定一个文件\\server\share\filename
,如何获取图标?
注意: .NET 2.0
Bre*_*tra 30
用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
Run Code Online (Sandbox Code Playgroud)
Ian*_*oyd 21
为了完整起见,这是一个有效的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);
}
Run Code Online (Sandbox Code Playgroud)
注意:任何代码都将发布到公共域中.无需归属.
归档时间: |
|
查看次数: |
8316 次 |
最近记录: |