开发者

Find a files with wildcard

开发者 https://www.devze.com 2023-01-18 08:30 出处:网络
开发者_运维问答Hy, How to loop through files matching wildcardand find the file that was last date created.

开发者_运维问答Hy, How to loop through files matching wildcard and find the file that was last date created. This is in VB ... So I have some files that are with a specific prefix and I like to find the one that is last datetime created! How can his be done?

Thank you! Adrian


You could use the GetFiles method which takes a wildcard mapping pattern and returns the files. If you are using .NET 4.0 you could also use the EnumerateFiles method which returns an IEnumerable<string> instead of an array. Once you get the files you could apply your filtering logic using LINQ extension methods to order the collection by file creation time and get the first element.

Dim result = Directory.
    EnumerateFiles("c:\test", "*.txt", SearchOption.TopDirectoryOnly).
    OrderByDescending(Function(file) New FileInfo(file).CreationTime).
    FirstOrDefault()
0

精彩评论

暂无评论...
验证码 换一张
取 消