【问题标题】:bring multi extension files and stop looping them when the list reach the end - c#带多个扩展文件并在列表到达末尾时停止循环它们 - c#
【发布时间】:2017-01-28 03:32:39
【问题描述】:

我有一个简单的软件循环multi Images/videos
并使用 (Next)-(previous) 按钮一一展示

到目前为止,我成功地一次只使用一个扩展名:

string[] FileList = Directory.GetFiles(Dir, "*.jpg", SearchOption.AllDirectories);

我尝试使用以下代码在列表到达末尾时停止循环:

int currentPosition = 0;
if (currentPosition != ImageList.Length) currentPosition++;

请原谅我的错误,因为我在 c# 中仍然是菜鸟,但是当我调试它时说
> IndexOutOfRangeException

在我添加多个这样的扩展之后:

    string[] VOut_AVI = Directory.GetFiles(@Curr_DirectoryName, "*.avi", SearchOption.AllDirectories);
    string[] VOut_MP4 = Directory.GetFiles(@Curr_DirectoryName, "*.mp4", SearchOption.AllDirectories);
    string AddMP4 = VOut_MP4[VOut_MP4.Length - 1];
    //------ Adding other format to our call ------------
    Array.Resize(ref VOut_AVI, VOut_AVI.Length + 1);
    VOut_AVI[VOut_AVI.Length - 1] = AddMP4;

    // just for test ----->>
    MessageBox.Show("Video link : " + VOut_AVI[currentPosition]);

我收到了这个错误(XamlParserException)。

如果您知道获得多个扩展的简单方法,而无需 像我一样使用那么多编码我会很高兴知道它 如果你能引导我走正确的路,我的循环过程也不是很好,我还在学习这些东西,这对我来说有点困难,因为我来自一个完全不同的环境,提前谢谢

【问题讨论】:

    标签: c# listview directory getfiles filelist


    【解决方案1】:

    要获取具有不同扩展名的文件名列表,请使用 Directory.EnumerateFiles 并连接这些查询。

    IEnumerable<string> myFilesQuery = new string[] { };
    
    myFilesQuery = myFilesQuery.Concat( Directory.EnumerateFiles( dir, "*.avi", SearchOption.AllDirectories ) );
    myFilesQuery = myFilesQuery.Concat( Directory.EnumerateFiles( dir, "*.mp4", SearchOption.AllDirectories ) );
    myFilesQuery = myFilesQuery.Concat( Directory.EnumerateFiles( dir, "*.jpg", SearchOption.AllDirectories ) );
    
    // nothing will happen until we call
    
    IList<string> myFiles = myFilesQuery.ToList( );
    

    如果您希望代码只有一行代码,请编写一个类似的帮助程序

    public static class DirectoryHelper
    {
        public static IEnumerable<string> EnumerateFiles( 
            string path, 
            SearchOption searchOption, 
            params string[] searchPatterns )
        {
            if ( searchPatterns == null )
                throw new ArgumentNullException( nameof( searchPatterns ) );
    
            IEnumerable<string> result = new string[] { };
    
            foreach ( var searchPattern in searchPatterns )
            {
                result = result.Concat( Directory.EnumerateFiles( path, searchPattern, searchOption ) );
            }
    
            return result;
        }
    }
    

    在我们的代码中你只有

    IList<string> myFiles 
        = DirectoryHelper
            .EnumerateFiles( 
                dir, 
                SearchOption.AllDirectories, 
                "*.avi", "*.mp4", "*.jpg" )
            .ToList();
    

    您不必担心数组的任何长度。该列表将包含 Count 属性中包含的元素的数量。

    所有集合都是从零开始的,第一项的索引为 0,最后一项的索引为 Count-1(集合)或Length-1(数组)。当currentPosition &gt;= mylist.Count-1 到达终点,只要currentPosition &lt; mylist.Count-1 就可以增加索引。

    更新

    这里已经有一个关于 SO 用并行实现处理多个搜索模式的答案

    https://stackoverflow.com/a/3754470/1744164

    【讨论】:

    • 此代码与 Visual Studio 2015ff 完美结合。将 nameof(searchPatterns) 更改为字符串“searchPatterns”
    • 我已经给你答案了:列表使用myFiles.Count,数组使用myFiles.Length。你应该阅读文档 => 谷歌搜索“c# IList”将引导你到 MSDN
    • 谢谢 :) 如何按 创建日期 对我的文件进行排序?我用谷歌搜索,但它们比我想要的要复杂得多,而且它不适用于我的代码。我只想简单代码按日期对我的文件进行排序。
    • 查找 FileInfo 和 IEnumerable.OrderBy
    • 顺便说一句,您知道 Visual Studio 社区版吗?它几乎是 Pro 版(远非受限),对广大开发者免费,您可以获得 VS2013/2015 或全新的 2017RC 作为 CE
    【解决方案2】:

    Length 返回项目的数量,但将从索引 0,1 检索。

    例如,如果您的列表共有 2 条记录,那么您只能访问 0,1 索引中的项目。它在第二个索引中不可用,因此会引发错误。

    换成ImageList.Length-1

    要获取所有具有不同扩展名的文件,您可以使用扩展名类型为 * 的 EnumerateFiles。您也可以使用 where 子句过滤文件。

    var resufile = Directory.EnumerateFiles(@"fullpath", "*.*", SearchOption.AllDirectories)
                .Where(s => s.EndsWith(".mp4") || s.EndsWith(".jpg"));  //different extension you can specify it
    

    【讨论】:

    • @SUB-HDR 这是您收到错误的完整代码吗?分享完整的代码,很容易解释,我在那里找不到任何按钮
    • @SUB-HDR for ex: string[] arr = new string[] { "test1", "test2", "test3" };数组项包含 3 个值,因此长度为 3。但数组中项的起始索引是 0 而不是 1。因此访问数组中的项必须是 arr[0] = "tes1", arr[1] = "测试2”,arr [2] =“测试3”。如果您访问 arr[3] 中的项目,则会引发错误。因为 arr 不包含索引 3。
    • 使用 Directory.EnumerateFiles(...).Concat( Directory.EnumerateFiles(...)).ToList()
    • @BalajiMarimuthu :非常感谢您的解释 :)
    • @SirRufo 好的,谢谢你,我是如何设置这个代码的,像这样吗? : Directory.EnumerateFiles("c:\media").Concat( Directory.EnumerateFiles(".jpg",".png","mp4","avi")).ToList(‌​);
    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多