我想知道在VB.NET中是否有任何方法可以使用带有一维字符串的逻辑运算符。
这是我的代码的一部分,理想情况下,我希望能够组合这两种搜索(例如,file.GetFiles("*.mp4" And "*.wmv")):
For Each f In file.GetFiles("*.mp4")
FileBrowser.Items.Add(f.Name, 5)
i = FileBrowser.FindItemWithText(f.Name).Index
FileBrowser.Items.Item(i).Text = f.Name.Remove(f.Name.Count - f.Extension.Count, f.Extension.Count)
FileBrowser.Items.Item(i).Name = f.FullName
Next
For Each f In file.GetFiles("*.wmv")
FileBrowser.Items.Add(f.Name, 5)
i = FileBrowser.FindItemWithText(f.Name).Index
FileBrowser.Items.Item(i).Text = f.Name.Remove(f.Name.Count - f.Extension.Count, f.Extension.Count)
FileBrowser.Items.Item(i).Name = f.FullName
Next可以通过使用字符串数组或列表来完成吗?
发布于 2015-08-20 07:01:38
如果将每个文件扩展名放入一个数组中,您只需对每个扩展名进行迭代,在添加或删除扩展名时唯一需要更改的就是数组本身。
Dim LookForExts() As String = New String() {"*.mp4", "*.wmv", "*.mp3", "*.wav"} 'Add or remove file extensions here.
For Each ext In LookForExts
For Each f In file.GetFiles(ext)
FileBrowser.Items.Add(f.Name, 5)
i = FileBrowser.FindItemWithText(f.Name).Index
FileBrowser.Items.Item(i).Text = f.Name.Remove(f.Name.Count - f.Extension.Count, f.Extension.Count)
FileBrowser.Items.Item(i).Name = f.FullName
Next
Next发布于 2015-08-19 06:20:42
对于.NET 4.0及更高版本,
Dim files = Directory.EnumerateFiles("C:\path", "*.*", SearchOption.AllDirectories)
.Where(Function(s) s.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)
OrElse s.EndsWith(".wmv", StringComparison.OrdinalIgnoreCase))对于早期版本的.NET,
Dim files = Directory.GetFiles("C:\path", "*.*", SearchOption.AllDirectories)
.Where(Function(s) s.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)
OrElse s.EndsWith(".wmv", StringComparison.OrdinalIgnoreCase))注意,由于性能的提高,'Directory.EnumerateFiles()‘比'Directory.GetFiles()’更好。如果您的目录中没有大量的文件,那么Directory.GetFiles()方法将非常好地工作。
发布于 2015-08-19 07:35:26
您可以结合使用regex和Directory.EnumerateFiles。就像这样:
Regex re = new Regex("\.(mp4|wmv)$");
Dim filteredFiles = Directory.EnumerateFiles(directoryPath, "*.*", SearchOption.AllDirectories).Where(Function(c) re.IsMatch(c))https://stackoverflow.com/questions/32086168
复制相似问题