You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.Run or BackgroundWorker) to keep your UI responsive.
  • Root Directory: Instead of hardcoding the root, add a FolderBrowserDialog to let users select it dynamically—this makes your program more flexible.

内容的提问来源于stack exchange,提问作者buelianvaluz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:24:36