How to get contents of a directory without includi

2019-08-31 05:35发布

I want to get the contents of a directory as an array excluding system / hidden files and folders. FileSystem.GetDirectories(path) and FileSystem.GetFiles(path) returns all files included in the path. So how to exclude system/hidden files from it?

标签: vb.net file
2条回答
放荡不羁爱自由
2楼-- · 2019-08-31 05:57

Try this you will have to modify the linq query or just use the Directory Info Object Directly

Dim root As String = "X:\" 

        'Take a snapshot of the folder contents 
        Dim dir As New System.IO.DirectoryInfo(root)

        Dim fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories)

        ' This query will produce the full path for all .txt files 
        ' under the specified folder including subfolders. 
        ' It orders the list according to the file name. 
        Dim fileQuery = From file In fileList _
                        Where file.Extension = ".txt" and file.Length >1645 _
                        Order By file.Length _
                        Select file
查看更多
放荡不羁爱自由
3楼-- · 2019-08-31 06:00

I know it's an old question but here the solution!

FileSystem.GetFiles(path).Where(Function(file) ((file.Attributes And FileAttributes.Hidden) <> FileAttributes.Hidden) AndAlso (file.Attributes And FileAttributes.System) <> FileAttributes.System)

I just checked all files against the two flags you asked.

查看更多
登录 后发表回答