本文介绍了不能在String []数组排序从文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是记录画面的图像序列,然后,而不是让视频我刨去加载所有图像目录列表,然后使用定时器通过图像来查看他们的形象,但我得到了错误的顺序文件是这样的:

my project is about recording screen as sequence of images then instead of make it as video i planed to load all image directories to list and use timer to view them image by image, but i get files in wrong order like this:

这段代码是从目录中加载文件:

this code is to load files from directory:

string[] array1 = Directory.GetFiles("C:\\Secret\\" + label1.Text, "*.Jpeg");
Array.Sort(array1);

foreach (string name in array1)
{
    listBox1.Items.Add(name);
}
timer2.Start();

这代码来查看

        int x = 0;
    private void timer2_Tick(object sender, EventArgs e)
    {
        if (x >= listBox1.Items.Count)
        {
            timer2.Stop();
        }
        else
        {
            ssWithMouseViewer.Image = Image.FromFile(listBox1.Items[x].ToString());

            x++;
        }
    }



我需要为了像0.jpeg来查看,1.jpeg,2.jpeg ..... 10.jpeg,11..jpeg ...

i need to view them in order like 0.jpeg, 1.jpeg, 2.jpeg.....10.jpeg, 11..jpeg...

推荐答案

的串排序:按字典顺序...

The strings are sorted: in lexicographic order...

您有两种选择:重命名文件,使他们在字典顺序进行排序(如:001,002,003 .. ),或者使用LINQ和文件名操作:

you have two options: rename the files so they be ordered in lexicographic order (eg: 001, 002, 003...), or, using linq, and file name manipulations:

IEnumerable<string> sorted = from filename in array1
                             orderby int.Parse(Path.GetFileNameWithoutExtension(filename))
                             select filename;

这篇关于不能在String []数组排序从文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 08:00