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
|
public string[] SearchFile()
{
string[] files;
string[] fileFound = new string[] { string.Empty};
long z = 0;
string text=string.Empty;
string searchTerm = "test8";
files = Directory.GetFiles(Path);
int fileCount = files.GetUpperBound(0) + 1;
for (int i = 0; i < fileCount;i++ )
{
text = File.ReadAllText(Path + "\\" + System.IO.Path.GetFileName(files[i]));
//Convert the string into an array of words
string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
var matchQuery = from word in source
where word.ToLowerInvariant().Contains(searchTerm.ToLowerInvariant())
select word;
// Count the matches, which executes the query.
if (matchQuery.Count()>=1)
{
if (z>0)
{
string[] temp = new string[z + 1];
Array.Copy(fileFound, temp, Math.Min(fileFound.Length, temp.Length));
fileFound = temp;
fileFound[z] = System.IO.Path.GetFileName(files[i]);
}
else
{
fileFound[z] = System.IO.Path.GetFileName(files[i]);
}
z++;
}
} |
Partager