Advertisement
Guest User

Untitled

a guest
Jul 25th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Diagnostics;
  11.  
  12. namespace FindWord
  13. {
  14. public partial class Form1 : Form
  15. {
  16. string selectedPath = null;
  17. string word = null;
  18.  
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private void button1_Click(object sender, EventArgs e)
  25. {
  26. listBox1.Items.Clear();
  27.  
  28. if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  29. {
  30. word = textBox1.Text;
  31. if (word == "")
  32. return;
  33.  
  34. selectedPath = folderBrowserDialog1.SelectedPath;
  35. Recursion(selectedPath);
  36. }
  37. }
  38.  
  39. void Recursion(string originalPath)
  40. {
  41. foreach (string filePath in Directory.GetFiles(originalPath))
  42. if (Contains(filePath, word))
  43. listBox1.Items.Add(filePath);
  44.  
  45. foreach(string directoryPath in Directory.GetDirectories(originalPath))
  46. Recursion(directoryPath);
  47. }
  48.  
  49. bool Contains(string filePath, string word)
  50. {
  51. try
  52. {
  53. using (StreamReader sr = new StreamReader(filePath))
  54. if (sr.ReadToEnd().Contains(word))
  55. return true;
  56.  
  57. return false;
  58. }
  59.  
  60. catch { return false; }
  61. }
  62.  
  63. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  64. {
  65. string selectedPath = (string)listBox1.SelectedItem;
  66. if (selectedPath == null)
  67. return;
  68.  
  69. try
  70. {
  71. Process.Start(selectedPath);
  72. }
  73.  
  74. catch { }
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement