Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I am adding files to listview from openfiledialog, and I want to add them with icons for different files , perhaps it reads the file extension I am adding, and I have this code.
C#
ImageList imageList1 = new ImageList();
System.IO.DirectoryInfo document = new System.IO.DirectoryInfo(openFileDialog2.FileName);
ListViewItem item;
listOfLoadedFiles.BeginUpdate();
foreach (FileInfo file in document .GetFiles())
{    
    Icon iconForFile = SystemIcons.WinLogo;
    item = new ListViewItem(file.Name, 1);
    iconForFile = Icon.ExtractAssociatedIcon(file.FullName);    
    if (!imageList1.Images.ContainsKey(file.Extension)){        
        iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName);
        imageList1.Images.Add(file.Extension, iconForFile);
    }
    item.ImageKey = file.Extension;
    listOfLoadedFiles.Items.Add(item);
}
listOfLoadedFiles.EndUpdate();

Put I get an error on the line.
C#
foreach (FileInfo file in dir.GetFiles())

It says The directory name is invalid.
Posted
Updated 28-Oct-13 0:41am
v2
Comments
GuyThiebaut 28-Oct-13 10:34am    
When you get directory information you will also need to handle the access denied exception - this may well be what is happening in this case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900