search for word in directory

1

I need in C # Forms, go through all the files in a folder and search for a word and that the program tells me the name of the files in which I can find this word ## faz. }

the code I have is

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace viernes16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

            string[] files = Directory.GetFiles(@"" + ruta);
            List<string> encontrados = new List<string>();

            foreach (string File in Directory.GetFiles(ruta, texto))
            {

                if (File.ReadAllText(File, Encoding.Default).ToUpper().Contains(texto.ToUpper()))
                    MessageBox.Show($"Archivo: {File}");
            }
        }
    }
}
    
asked by Jhon Chavez 15.06.2017 в 19:06
source

1 answer

1

with Directory.GetFiles(directorio, patron) , you can achieve what you are looking for this is within System.IO ;

example:

using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] archivos = Directory.GetFiles("c://", "*i*.*");
            foreach (string archivo in archivos)
            {
                Console.WriteLine(archivo);
            }
            Console.ReadKey();
        }
    }
}

pattern does not accept regex , only the wildcards * and?

    
answered by 15.06.2017 в 20:52