本文介绍了如何在Folderbrowserdialog中过滤文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用folderBrowserDialog用imagefiles填充listview。谁知道快速的方法呢?



谢谢:)

I try to fill a listview with imagefiles by an folderBrowserDialog. Who know a fast way to do this?

Thank you :)

推荐答案

if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
    this.listView1.View = View.LargeIcon;
    this.listView1.LargeImageList = new ImageList() { ImageSize = new Size(64, 64) };

    var imageFiles = from file in Directory.EnumerateFiles(this.folderBrowserDialog1.SelectedPath)
                    let extension = Path.GetExtension(file)
                    // Add more image extensions if needed ... 
                    where extension.Equals(".jpg") || extension.Equals(".png")
                    select file;

    int imageIndex = 0;
    foreach (string imageFile in imageFiles)
    {
        this.listView1.LargeImageList.Images.Add(Image.FromFile(imageFile));
        this.listView1.Items.Add(null, imageIndex++);
    }
}


// I moved this outside.
private int imageIndex = 0;

public Form1()
{
    InitializeComponent();

    this.listView1.AllowDrop = true;

    this.listView1.DragEnter += (sender, e) =>
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.Copy;
    };

    this.listView1.DragDrop += (sender, e) =>
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

        // You can notice that the following lines are repetition which I also used in above sample code.
        // So you can tweak a bit your code so that you don't repeat yourself.
        var imageFiles = from file in files
                            let extension = Path.GetExtension(file)
                            where extension.Equals(".jpg") || extension.Equals(".png")
                            select file;

        foreach (string imageFile in imageFiles)
        {
            this.listView1.LargeImageList.Images.Add(Image.FromFile(imageFile));
            this.listView1.Items.Add(null, this.imageIndex++);
        }
    };
}


这篇关于如何在Folderbrowserdialog中过滤文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:00