简体   繁体   English

在vb.net中,如何基于逗号分隔的字符串从目录中获取文件?

[英]In vb.net, how do I get files from a directory based on a comma separated string?

I need to create an array() from files in a folder. 我需要从文件夹中的文件创建一个array() Here's an example of how I would get all files within a folder. 这是我如何获取文件夹中所有文件的示例。

Dim filesList = New DirectoryInfo("MyPath").GetFiles("*", SearchOption.TopDirectoryOnly).Where(Function(f) Not f.Attributes.HasFlag(FileAttributes.Hidden)).[Select](Function(f) New AClassNameHere(f)).ToArray()

I want to do the exact same thing, but only get files that exist in a comma separated string. 我想做完全相同的事情,但只获取以逗号分隔的字符串中存在的文件。

Dim myFiles as String = "filename1.jpg,filename2.jpg,filename3.jpg"

Where you see the AClassNameHere is a class I need to send each file to, and it would also be great if I knew how to send additional data about each file, like its type, size, etc. 您在哪里看到AClassNameHere是一个我需要将每个文件发送到的类,如果我知道如何发送有关每个文件的其他数据(例如文件的类型,大小等),那也将非常好。

Thank you kindly! 非常感谢你!

You could narrow the query results by adding an additional .Where() filter 您可以通过添加其他.Where()过滤器来缩小查询结果的范围

Dim myFiles as String = "filename1.jpg,filename2.jpg,filename3.jpg"
Dim filesList = New DirectoryInfo("MyPath")
                .GetFiles("*", SearchOption.TopDirectoryOnly)
                .Where(Function(f) Not f.Attributes.HasFlag(FileAttributes.Hidden))
                .Where(Function(f) myFiles.Contains(f.Name))
                .[Select](Function(f) New AClassNameHere(f)).ToArray()

A better option would be to ensure that all filenames follow a pattern. 更好的选择是确保所有文件名都遵循模式。

New DirectoryInfo("MyPath").GetFiles("filename*.jpg", SearchOption.TopDirectoryOnly)

Use this... 用这个...

    Dim Files() As String
    Files= filesList.Split(",")

    For each File In Files
    Msgbox(File)
    Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM