다음을 통해 공유


방법: Windows Forms에서 파일과 연결된 아이콘 추출

업데이트: 2007년 11월

많은 파일에는 연관된 파일 형식의 시각적 표현을 제공하는 아이콘이 포함되어 있습니다. 예를 들어, 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 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 (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 네임스페이스를 가져오는지 확인해야 합니다.

참고 항목

기타 리소스

이미지, 비트맵 및 메타파일

ListView 컨트롤(Windows Forms)