How to get the associated icon from a network share file

Viewed 10523

I am using Icon.ExtractAssociatedIcon to get the icon of a file , that a user selects, in an openfiledialog.

THe problem is if the user selects an icon from a network share then the filename property of the openfiledialog is in UNC format and this causes an ArgumentException in ExtractAssocaitedIcon:

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

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

So my question is given a file specified as \\server\share\filename, how do I get the icon?

Note: .NET 2.0

5 Answers

Just to complement Ian Boyd answer , if the file is an image, instead of the file icon, you can use FileStream to preview it:

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));

     }
}
Related