VB中基于通配符搜索文件夹并导入文件至ListBox的问题排查
Got it, let's fix this issue step by step. The core problem here is that you need to perform a partial, wildcard-based folder match based on your TextBox input, then grab video files from those matching folders to populate your ListBox. Here's how to make this work smoothly:
1. Fix your wildcard matching logic
When you type "Cats and D" into the TextBox, you can't use that string directly as a search pattern—you need to wrap it with wildcards to catch folders that contain that substring. Use *Cats and D* instead: the asterisks act as placeholders for any characters before, after, or in between your input. This will match folders like "Cats and Dogs Funnies" perfectly.
2. Search for matching parent folders
First, target the root directory where your folders are stored (you can hardcode this or let users select it via a folder picker). Use Directory.EnumerateDirectories with your wildcard pattern to fetch all folders that match the TextBox input.
3. Grab video files from matching folders
Define the video file extensions you want to target (like .mp4, .avi, .mov), then scan each matching folder (and its subfolders, if needed) for these files. Use Directory.EnumerateFiles with the right search option to cover subfolders.
4. Populate your ListBox
Clear existing items first, then add each video file's full path to the ListBox. If you're working with WinForms/WPF, make sure you're updating the UI on the main thread to avoid cross-thread errors.
Example Code (C# WinForms)
private void btnSearchVideos_Click(object sender, EventArgs e) { // Clear old entries first listBoxVideoPaths.Items.Clear(); // Set up your search parameters string userInput = textBoxSearch.Text.Trim(); if (string.IsNullOrEmpty(userInput)) { MessageBox.Show("Please enter a search term!"); return; } string searchPattern = $"*{userInput}*"; string rootDirectory = @"C:\Your\Root\Folder\Path"; // Adjust this to your actual root string[] videoExtensions = { "*.mp4", "*.avi", "*.mov", "*.mkv", "*.wmv" }; try { // Find all folders matching the user's input var matchingFolders = Directory.EnumerateDirectories(rootDirectory, searchPattern, SearchOption.TopDirectoryOnly); foreach (var folder in matchingFolders) { // Loop through each video extension to collect files foreach (var ext in videoExtensions) { // Grab all video files in the folder and its subfolders var videoFiles = Directory.EnumerateFiles(folder, ext, SearchOption.AllDirectories); foreach (var file in videoFiles) { // Add the full file path to the ListBox listBoxVideoPaths.Items.Add(file); } } } } catch (Exception ex) { MessageBox.Show($"Oops, something went wrong: {ex.Message}"); } }
Quick Notes to Avoid Issues
- Case Sensitivity: On Windows, directory searches are case-insensitive by default. If you need case-sensitive matching, add custom checks using
StringComparison.Ordinal. - UI Freezing: If you're searching large directories, run the search in a background thread (using
Task.RunorBackgroundWorker) to keep your UI responsive. - Root Directory: Instead of hardcoding the root, add a
FolderBrowserDialogto let users select it dynamically—this makes your program more flexible.
内容的提问来源于stack exchange,提问作者buelianvaluz

