Use iconhandler 2.0 to get the file icon

Source: Internet
Author: User
Tags fiddler2

Http://www.cnblogs.com/ldp615/archive/2010/09/26/IconHandler.html

Use iconhandler 2.0 to get the file icon

By Crane chongtian, 2634 read, 21 comments, favorites, Edit

In Windows Resource Manager, we can use icons to view files. The icons allow us to quickly determine the file type, such as the selected file, I believe you can see at a glance that it is a Word document.

Beautiful design icons make us look pleasing to the eye. If you can use these small icons directly in the program we write, it will certainly increase the color.

However. net does not directly provide us with a way to directly obtain the file icon. If you use the search engine to find a solution, you will find many solutions, but most of them are provided with scattered code, and the quality and source are not guaranteed.

I found an iconhandler written by a foreign Daniel and shared it with you!

Iconhandler

Iconhandler is a class library shared by mvolo. It is currently in version 2.0. The URL is:

Http://mvolo.com/blogs/serverside/archive/2008/04/27/IconHandler-2.0-File-icons-in-ASP.NET-applications.aspx

This address is invalid New Address: http://mvolo.com /? S = iconhandler & X = 13 & Y = 10

The downloaded package contains the following two DLL files:

The first file, shellicons. dll, is used to retrieve icons from the system. The second file, shelliconhandler. dll, is used to display icons in ASP. NET. First look at the first file.

Shellicons. dll

To retrieve the icon, we only need to call the shellicons. geticonforfile static method. This method has three parameters:

public class ShellIcons{    public static IconContainer GetIconForFile(string path, bool useExtension, bool largeIcon);}

Let's take a look at the two pieces of code. The meaning of these three parameters will naturally be understood:

// Winform: Get the big icon of the RAR file and display it on picturebox1 using (VAR Container = shellicons. geticonforfile (". RAR ", true, true) {icon rarfilelargeicon = container. icon; bitmap rarfilelargebmp = rarfilelargeicon. tobitmap (); picturebox1.image = rarfilelargebmp;} // winform: Get the small fiddler.exe icon and display it on picturebox2 using (VAR Container = shellicons. geticonforfile (@ "C: \ Program Files \ fiddler2 \ fiddler.exe", false, false) {picturebox2.image = container. icon. tobitmap ();}

Shown as follows:

 

Shelliconhandler. dll

This dll is used to display images in ASP. NET. Let's first look at an image in the author's file:

This call is cool and easy to use. First, reference the above two DLL files, and then modify the Web. config file (add the underlined part ):

<configuration>    <!-- ShellIconHandler configuration section declaration -->     <configSections>        <section name="iconHandler" type="Mvolo.ShellIcons.Web.ShellIconHandlerConfigurationSection" />    </configSections>    <system.webServer>        <!-- Add IconHandler for IIS 7.0 Integrated mode -->        

OK. We will try to display several icons on the home page:

<br />

Shown as follows:

Good results! (You can also use the iconhandler section option in the configuration file to specify whether the cache is performed .)

Here, some may ask, the website is usually deployed on the server. Can the icons correctly display if the server does not have office installed? Unfortunately, it cannot be displayed. iconhandler uses Shell APIShgetfileinfoIf the file type is not registered in the system, it cannot be obtained. Fortunately, mvolo has taken this issue into consideration and provided us with a solution:

Create a new project named/App_resources/iconsTo put the image in the following ways:

Modify the iconhandler configuration section (add the underlined section ):

<configuration>    <iconHandler enabled="true"                alwaysUseExtension="true"                enableClientCaching="true"                enableServerCaching="true"                useSavedIcons="true" /></configuration>

Let's test again and add the following code to the homepage:

<br />

Preview as follows:

But there may be hundreds or even thousands of file types, which is too troublesome. Fortunately, mvolo also provides us with another tool.

Icongen.exe

Using this tool, we can generate the corresponding size icons for files in batches. This is a control platform application. You only need to execute the following two commands:

> IconGen.exe c:\Icons large> IconGen.exe c:\Icons small

Generate the size icons respectively, as shown below:

A total of 2038 icons are generated, large and small!

 

Download: iconhandler 2.0 (10kb) icongen.exe (25kb)

Local download: iconhandler 2.0 (10kb) icongen.exe (25kb)

In Windows Resource Manager, we can use icons to view files. The icons allow us to quickly determine the file type, such as the selected file, I believe you can see at a glance that it is a Word document.

Beautiful design icons make us look pleasing to the eye. If you can use these small icons directly in the program we write, it will certainly increase the color.

However. net does not directly provide us with a way to directly obtain the file icon. If you use the search engine to find a solution, you will find many solutions, but most of them are provided with scattered code, and the quality and source are not guaranteed.

I found an iconhandler written by a foreign Daniel and shared it with you!

Iconhandler

Iconhandler is a class library shared by mvolo. It is currently in version 2.0. The URL is:

Http://mvolo.com/blogs/serverside/archive/2008/04/27/IconHandler-2.0-File-icons-in-ASP.NET-applications.aspx

This address is invalid New Address: http://mvolo.com /? S = iconhandler & X = 13 & Y = 10

The downloaded package contains the following two DLL files:

The first file, shellicons. dll, is used to retrieve icons from the system. The second file, shelliconhandler. dll, is used to display icons in ASP. NET. First look at the first file.

Shellicons. dll

To retrieve the icon, we only need to call the shellicons. geticonforfile static method. This method has three parameters:

public class ShellIcons{    public static IconContainer GetIconForFile(string path, bool useExtension, bool largeIcon);}

Let's take a look at the two pieces of code. The meaning of these three parameters will naturally be understood:

// Winform: Get the big icon of the RAR file and display it on picturebox1 using (VAR Container = shellicons. geticonforfile (". RAR ", true, true) {icon rarfilelargeicon = container. icon; bitmap rarfilelargebmp = rarfilelargeicon. tobitmap (); picturebox1.image = rarfilelargebmp;} // winform: Get the small fiddler.exe icon and display it on picturebox2 using (VAR Container = shellicons. geticonforfile (@ "C: \ Program Files \ fiddler2 \ fiddler.exe", false, false) {picturebox2.image = container. icon. tobitmap ();}

Shown as follows:

 

Shelliconhandler. dll

This dll is used to display images in ASP. NET. Let's first look at an image in the author's file:

This call is cool and easy to use. First, reference the above two DLL files, and then modify the Web. config file (add the underlined part ):

<configuration>    <!-- ShellIconHandler configuration section declaration -->     <configSections>        <section name="iconHandler" type="Mvolo.ShellIcons.Web.ShellIconHandlerConfigurationSection" />    </configSections>    <system.webServer>        <!-- Add IconHandler for IIS 7.0 Integrated mode -->        

OK. We will try to display several icons on the home page:

<br />

Shown as follows:

Good results! (You can also use the iconhandler section option in the configuration file to specify whether the cache is performed .)

Here, some may ask, the website is usually deployed on the server. Can the icons correctly display if the server does not have office installed? Unfortunately, it cannot be displayed. iconhandler uses Shell APIShgetfileinfoIf the file type is not registered in the system, it cannot be obtained. Fortunately, mvolo has taken this issue into consideration and provided us with a solution:

Create a new project named/App_resources/iconsTo put the image in the following ways:

Modify the iconhandler configuration section (add the underlined section ):

<configuration>    <iconHandler enabled="true"                alwaysUseExtension="true"                enableClientCaching="true"                enableServerCaching="true"                useSavedIcons="true" /></configuration>

Let's test again and add the following code to the homepage:

<br />

Preview as follows:

But there may be hundreds or even thousands of file types, which is too troublesome. Fortunately, mvolo also provides us with another tool.

Icongen.exe

Using this tool, we can generate the corresponding size icons for files in batches. This is a control platform application. You only need to execute the following two commands:

> IconGen.exe c:\Icons large> IconGen.exe c:\Icons small

Generate the size icons respectively, as shown below:

A total of 2038 icons are generated, large and small!

 

Download: iconhandler 2.0 (10kb) icongen.exe (25kb)

Local download: iconhandler 2.0 (10kb) icongen.exe (25kb)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.