Приложение, которое ищет файлы по заданному шаблону имени файла и по заданной фразе для внутреннего содержимого файла.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.4 KiB

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace fileFinder
{
public enum TaskState { Created, Work, Pause, Finished }
class TaskController
{
private Stopwatch stopwatch = new Stopwatch();
public TaskState state { get; private set; }
public TaskController()
{
state = TaskState.Created;
}
public void beginTask()
{
state = TaskState.Work;
stopwatch.Restart();
}
public void pauseTask()
{
state = TaskState.Pause;
stopwatch.Stop();
}
public void resumeTask()
{
state = TaskState.Work;
stopwatch.Start();
}
public void stopTask()
{
state = TaskState.Finished;
stopwatch.Stop();
}
public TimeSpan elapsedTime()
{
return stopwatch.Elapsed;
}
public async Task<TreeNode> buildResultTree(SearchQueryModel query, IProgress <ProgressReportModel> report)
{
TaskController controller = this;
TreeNode itemsNode = new TreeNode();
ProgressReportModel _report = new ProgressReportModel();
List<string> foundFiles = getFileList(query.fileUrl, query.fileNameQuery);
int counter = 0;
foreach (string item in foundFiles)
{
switch (state)
{
case TaskState.Finished:
return null;
case TaskState.Pause:
_report.progress = counter;
_report.currentFileUrl = item;
_report.currentTreeNode = (TreeNode)itemsNode.Clone();
report.Report(_report);
while (state == TaskState.Pause)
Thread.Sleep(100);
goto case TaskState.Work;
case TaskState.Work:
string[] fileLines = getFileContents(item);
_report.progress = counter;
_report.currentFileUrl = item;
report.Report(_report);
if ((fileLines.Length > 0) && (isFileContainQuery(fileLines, query.fileInnerQuery)))
{
counter++;
await fillChildNode(itemsNode, item.Replace(query.fileUrl, ""));
}
break;
}
}
controller.stopTask();
return itemsNode;
}
private List<string> getFileList(string directory, string nameQuery)
{
List<String> fileList = new List<String>();
try
{
fileList = Directory.GetFiles(directory, nameQuery, SearchOption.AllDirectories).ToList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Не удалось получить доступ к файлу/папке");
}
return fileList;
}
private string[] getFileContents(string fileUrl)
{
try
{
return File.ReadAllLines(fileUrl, Encoding.UTF8);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка поиска внутри файла!");
}
return null;
}
private bool isFileContainQuery(string[] fileLines, string query)
{
foreach (string item in fileLines)
if (item.IndexOf(query) != -1)
return true;
return false;
}
public async Task fillChildNode(TreeNode node, String item)
{
int backSlashIndex = item.IndexOf("\\");
switch (backSlashIndex)
{
case -1:
node.Nodes.Add(item, item, 2, 2);
break;
case 0:
item = item.Remove(0, 1);
await fillChildNode(node, item);
break;
default:
String currentNodeName = item.Substring(0, backSlashIndex);
int nodeIndex = node.Nodes.IndexOfKey(currentNodeName);
if (nodeIndex != -1)
await fillChildNode(node.Nodes[nodeIndex], item.Remove(0, backSlashIndex + 1));
else
{
node.Nodes.Add(currentNodeName, currentNodeName, 0, 0);
nodeIndex = node.Nodes.IndexOfKey(currentNodeName);
await fillChildNode(node.Nodes[nodeIndex], item.Remove(0, backSlashIndex + 1));
}
break;
}
}
public SearchQueryModel updateSearchQueryModel(string fileUrl, string fileNameQuery, string fileInnerQuery)
{
SearchQueryModel sqModel = new SearchQueryModel();
sqModel.fileUrl = fileUrl;
sqModel.fileNameQuery = fileNameQuery;
sqModel.fileInnerQuery = fileInnerQuery;
return sqModel;
}
}
}