Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Diagnostics;
- namespace FindWord
- {
- public partial class Form1 : Form
- {
- string selectedPath = null;
- string word = null;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
- {
- word = textBox1.Text;
- if (word == "")
- return;
- selectedPath = folderBrowserDialog1.SelectedPath;
- Recursion(selectedPath);
- }
- }
- void Recursion(string originalPath)
- {
- foreach (string filePath in Directory.GetFiles(originalPath))
- if (Contains(filePath, word))
- listBox1.Items.Add(filePath);
- foreach(string directoryPath in Directory.GetDirectories(originalPath))
- Recursion(directoryPath);
- }
- bool Contains(string filePath, string word)
- {
- try
- {
- using (StreamReader sr = new StreamReader(filePath))
- if (sr.ReadToEnd().Contains(word))
- return true;
- return false;
- }
- catch { return false; }
- }
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- string selectedPath = (string)listBox1.SelectedItem;
- if (selectedPath == null)
- return;
- try
- {
- Process.Start(selectedPath);
- }
- catch { }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement