HOW TO:擷取與 Windows Form 中檔案相關的圖示

許多檔案都有可提供相關聯檔案類型之視覺表示的內嵌圖示。 例如,Microsoft Word 文件就包含可將其識別為 Word 文件的圖示。 在清單控制項或表格控制項中顯示檔案時,您必須在每個檔案名稱的旁邊顯示代表檔案類型的圖示。 這時您只要使用 ExtractAssociatedIcon 方法,便可輕易完成這個動作。

範例

下列程式碼範例會示範如何擷取與檔案關聯的圖示,並在 ListView 控制項中顯示檔案名稱及其關聯的圖示。

Private listView1 As ListView
Private imageList1 As ImageList


Public Sub ExtractAssociatedIconEx()

    ' Initialize the ListView, ImageList and Form.
    listView1 = New ListView()
    imageList1 = New ImageList()
    listView1.Location = New Point(37, 12)
    listView1.Size = New Size(161, 242)
    listView1.SmallImageList = imageList1
    listView1.View = View.SmallIcon
    Me.ClientSize = New System.Drawing.Size(292, 266)
    Me.Controls.Add(Me.listView1)
    Me.Text = "Form1"

    ' Get the c:\ directory.
    Dim dir As New System.IO.DirectoryInfo("c:\")

    Dim item As ListViewItem
    listView1.BeginUpdate()
    Dim file As System.IO.FileInfo
    For Each file In dir.GetFiles()

        ' Set a default icon for the file.
        Dim iconForFile As Icon = SystemIcons.WinLogo

        item = New ListViewItem(file.Name, 1)

        ' Check to see if the image collection contains an image
        ' for this extension, using the extension as a key.
        If Not (imageList1.Images.ContainsKey(file.Extension)) Then

            ' If not, add the image to the image list.
            iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName)
            imageList1.Images.Add(file.Extension, iconForFile)
        End If
        item.ImageKey = file.Extension
        listView1.Items.Add(item)

    Next file
    listView1.EndUpdate()
End Sub
ListView listView1;
ImageList imageList1;

public void ExtractAssociatedIconEx()
{
    // Initialize the ListView, ImageList and Form.
    listView1 = new ListView();
    imageList1 = new ImageList();
    listView1.Location = new Point(37, 12);
    listView1.Size = new Size(151, 262);
    listView1.SmallImageList = imageList1;
    listView1.View = View.SmallIcon;
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.listView1);
    this.Text = "Form1";

    // Get the c:\ directory.
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\");

    ListViewItem item;
    listView1.BeginUpdate();

    // For each file in the c:\ directory, create a ListViewItem
    // and set the icon to the icon extracted from the file.
    foreach (System.IO.FileInfo file in dir.GetFiles())
    {
        // Set a default icon for the file.
        Icon iconForFile = SystemIcons.WinLogo;

        item = new ListViewItem(file.Name, 1);
        iconForFile = Icon.ExtractAssociatedIcon(file.FullName);

        // Check to see if the image collection contains an image
        // for this extension, using the extension as a key.
        if (!imageList1.Images.ContainsKey(file.Extension))
        {
            // If not, add the image to the image list.
            iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName);
            imageList1.Images.Add(file.Extension, iconForFile);
        }
        item.ImageKey = file.Extension;
        listView1.Items.Add(item);
    }
    listView1.EndUpdate();
}

編譯程式碼

若要編譯此範例:

  • 將先前的程式碼貼入 Windows Form,並從表單的建構函式或 Load 事件處理方法呼叫 ExtractAssociatedIconExample 方法。

    您將需要確定您的表單有匯入 System.IO 命名空間 (Namespace)。

請參閱

其他資源

影像、點陣圖和中繼檔

ListView 控制項 (Windows Form)