-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSearcher.cs
142 lines (104 loc) · 3.09 KB
/
FileSearcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* FileSearcher.java
*
* Created on 9 de Março de 2007, 14:21
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
using System.IO;
using System;
using System.Collections.Generic;
namespace SimpleFileSearch
{
/**
*
* @author gustavoh
*/
public class FileSearcher
{
public event Estado estado;
/** Creates a new instance of FileSearcher */
public FileSearcher()
{
}
/**
*
* @param path
* @param filename
* @param infile
* @return
*/
public List<FileInfo> SearchFiles(String path, String filename, String infile, bool CaseSens)
{
List<FileInfo> al = new List<FileInfo>();
bool bSearchIn = (infile != "");
try
{
string[] files = Directory.GetFiles(path, filename, SearchOption.AllDirectories);
estado.Invoke("Verifying files...");
int Max = files.Length;
int cnt =0;
int show = 0;
foreach (string sf in files)
{
cnt++;
if (bSearchIn)
{
if (SearchInFile(sf, infile, CaseSens))
{
al.Add(new FileInfo(sf));
}
}
else
{
al.Add(new FileInfo(sf));
}
show++;
if(show > 50){
show = 0;
estado.Invoke("Verifying files, "+ cnt + " de " + Max + " ...");
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return al;
}
private bool SearchInFile(string file, String searchWord, bool CaseSens)
{
try
{
using (StreamReader sr = File.OpenText(file))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (CaseSens)
{
if (line.Contains(searchWord))
{
return true;
}
}
else {
if (line.IndexOf(searchWord,StringComparison.InvariantCultureIgnoreCase) > 0)
{
return true;
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
return false;
}
return false;
}
}
public delegate void Estado(string value);
}