Search for text in a C # Path

0

I have a winform application to search for a certain text within a file path.
The problem is that it takes a lot in some searches and in Sometimes it falls. It would be convenient if someone can help me improve the performance of it, I share the code:

  private void btnexplorador_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtruta.Text = fbd.SelectedPath;
            }
        }

        private void btnbuscar_Click(object sender, EventArgs e)
        {
            lbxlista.Items.Clear();
            string ruta = @"" + txtruta.Text; //Escribir ruta
            string texto = txtfiltro.Text; //Escribir texto a buscar

            string[] files = Directory.GetFiles(ruta, "*", SearchOption.AllDirectories);
            //List<string> encontrados = new List<string>();
            foreach (string item in files)
            {
                string contenido = File.ReadAllText(item);

                if (contenido.Contains(texto))

                    lbxlista.Items.Add(item);
            }

        }
    
asked by Jhon Chavez 06.07.2017 в 21:23
source

1 answer

0

There is a .NET Framework method that provides this functionality. This method is FileSystem.FindInFiles Method ( String, String, Boolean, SearchOption) . It is true that this method is found in the namespace Microsoft.VisualBasic.FileIO in the assembly Microsoft.VisualBasic (en Microsoft.VisualBasic.dll) . The implementation in C # would be something like this:

/// <summary>
/// Get a list of files based on filename-with-wildcard search criteria and file-content search criteria.
/// Regular expressions are not supported (yet).
/// Calls System.IO.Directory.GetFiles to get files.
/// Calls System.IO.File.ReadAllText().Contains to search contents.
/// Uses ToLower() to perform case-insensitive search.
/// </summary>
/// <param name="directoryArg">Directory to start search, such as @"C:\" or Environment.GetEnvironmentVariable("SystemRoot")</param>
/// <param name="containsTextArg">Test to search for. "" will be found in any file.</param>
/// <param name="ignoreCaseArg"></param>
/// <param name="searchSubDirsArg"></param>
/// <param name="fileWildcardsArg">Can be an array of files or a single file such as "*.ini"</param>
/// <returns>a list of files (complete paths) found.</returns>
static IEnumerable<string> FindInFiles( 
    string                 directoryArg,
    string                 containsTextArg,
    bool                   ignoreCaseArg,
    System.IO.SearchOption searchSubDirsArg,
    params string[]        fileWildcardsArg )
{

    IEnumerable<string> files =
        from fileWildcard in fileWildcardsArg
        from file in System.IO.Directory.GetFiles(directoryArg, fileWildcard, searchSubDirsArg)
        where
            containsTextArg.Length == 0 ||
            ignoreCaseArg ?
            System.IO.File.ReadAllText(file).ToLower().Contains(containsTextArg.ToLower()) :
            System.IO.File.ReadAllText(file).Contains(containsTextArg)
        select file;

    return files;
}

And an example of a call would be the following:

IEnumerable oemFiles = FindInFiles( Environment.GetEnvironmentVariable("SystemRoot"), "VID_08b1", false, System.IO.SearchOption.AllDirectories, "OEM*.INF" );
    
answered by 02.04.2018 в 22:50