The problem of the following nature. There is a ListView list where items are added that contain the name and path to the directory. When adding an item to the list, an icon is automatically generated next to it (default) - this has already been done.
Subsequently, along the path that is specified in the item, the folder with the files will move from one place to another.

I need to, after the folder has been copied, the default icon is replaced by the one in the folder. If there is no icon in the folder, the default one remains.

  • WinForms or WPF? - VladD
  • added winforms - Rise Against

1 answer 1

So ListView has properties for icons - SmallImageList , StateImageList , LargeImageList which are used depending on the ListView display mode ( ListView property). When ImageList added to the image, you can specify an ImageKey , and for ListView.Items you can use this property to link the icon and the ListView element.

Accordingly, we solve the problem, for example, like this:

 ... //НазначаСм ImageList для ListView, Ссли Π΅Ρ‰Π΅ Π½Π΅ сдСлали этого Π² студии: listView1.LargeImageList = imageList1; //Π—Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ Π΄Π΅Ρ„ΠΎΠ»Ρ‚Π½ΡƒΡŽ ΠΈΠΊΠΎΠ½ΠΊΡƒ Π² ImageList: System.Drawing.Image defaultIcon = Image.FromFile(@"D:\Default.ico"); imageList1.Images.Add("DefaultIcon", defaultIcon); //ΠŸΡƒΡ‚ΡŒ ΠΊ ΠΈΠΊΠΎΠ½ΠΊΠ΅: string fi = @"D:\SomeFolder\Icon.ico"; //Имя ΠΈΠΊΠΎΠ½ΠΊΠΈ string iconKey = Path.GetFileNameWithoutExtension(fi); //Π—Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ ΠΈΠΊΠΎΠ½ΠΊΡƒ Π² ImageList if (File.Exists(fi)) { System.Drawing.Image folderIcon = Image.FromFile(fi); imageList1.Images.Add(iconKey, folderIcon); } //Если ΠΈΠΊΠΎΠ½ΠΊΠ° Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½Π°, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ ΠΈΠΊΠΎΠ½ΠΊΡƒ ΠΏΠΎ-ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ: else {iconKey = "DefaultIcon"} //ΠŸΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ ΠΈΠΊΠΎΠ½ΠΊΡƒ ΠΊ элСмСнту ListView: listView1.Items[0].ImageKey = iconKey; ... 

If for some reason you need to remove the icon from the ImageList by its key, you can do it like this:

 imageList1.Images.RemoveByKey(iconKey);