views:

31

answers:

1

I've seen a lot of code for doing listbox or listview thumbnails with hard coded, or submitted paths to an image. How would one just provide a path and c# display the contents of the directory as thumbnails?

Thanks for any help, I'm a total newb, my apologies if this is a lame question.

A: 

Ok this is derived from the sample Listviews with Multiple Views. I added a button which chooses a file in a directory and then gets all the files from the directory and adds them to the list view. I generically set every item to the cat image.

using Microsoft.Win32;
using System.Collections.Generic;

lv.ItemsSource = null;
List<XmlElement> elements = new List<XmlElement>();
Microsoft.Win32.FileDialog fd = new Microsoft.Win32.OpenFileDialog();
bool ?a = fd.ShowDialog();
if (a == true)
{
     XmlDocument doc = new XmlDocument();
     string dir = fd.FileName.Remove(fd.FileName.Length - fd.SafeFileName.Length, fd.SafeFileName.Length);
     string[] files = Directory.GetFiles(dir);
     foreach (string file in files)
     {
           XmlElement item = doc.CreateElement(file.Remove(0,dir.Length));
           item.SetAttribute("Name", file.Remove(0, dir.Length));
           item.SetAttribute("Type", Path.GetExtension(file));
           item.SetAttribute("Image", "images\\cat.png");
           elements.Add(item);
      }
}
lv.ItemsSource = elements;
Jack B Nimble
Thanks I'll try it.
rd42
using System.IO;FolderBrowserDialog my System.IO doesn't have FolderBrowserDialogif (fbd.ShowDialog() == DialogResult.OK) yields: Error 7 'System.Nullable<bool>' does not contain a definition for 'OK' and no extension method 'OK' accepting a first argument of type 'System.Nullable<bool>' could be found (are you missing a using directive or an assembly reference?)listView1.View = View.LargeIcon; - the name view does not exist in the current contextlvi = new ListViewItem(file); Error 9 'System.Windows.Controls.ListViewItem' does not contain a constructor that takes 1 arguments
rd42
Sorry for the mess, it's too bad comments can't be formatted.
rd42
It's because I'm in WPF...
rd42
Please see my revised solution.
Jack B Nimble