/* MainWindow.xaml */ using System; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows; namespace PhotoViewer { /// /// MainWindow.xaml の相互作用ロジック /// public partial class MainWindow : Window { private ObservableCollection photos = new ObservableCollection(); public MainWindow() { InitializeComponent(); this.Loaded += MainWindow_Loaded; this.PhotoListBox.ItemsSource = photos; } private static bool IsImageFile(string fileName) { // 適当 var ext = Path.GetExtension(fileName).ToLowerInvariant(); return ext == ".png" || ext == ".jpg"; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { Task.Factory.StartNew(() => { foreach (var path in Directory.EnumerateFiles(".").Where(IsImageFile)) { Dispatcher.BeginInvoke(new Action(() => { photos.Add(new PhotoInfo { FilePath = Path.GetFullPath(path) }); })); } }); } } public class PhotoInfo { public string FilePath { get; set; } public string Caption { get { return Path.GetFileName(FilePath); } } } }