Приложение, которое ищет файлы по заданному шаблону имени файла и по заданной фразе для внутреннего содержимого файла.
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.

183 lines
5.7 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.Timers;
using System.Windows.Forms;
namespace fileFinder
{
class TaskController
{
public bool isPaused { get; private set; }
public bool isStopped { get; private set; }
public bool isFormUpdatedAfterPause { get; set; }
public bool isTaskChanged { get; set; }
private Stopwatch stopwatch = new Stopwatch();
public TaskController ()
{
isPaused = false;
isStopped = true;
isFormUpdatedAfterPause = true;
isTaskChanged = false;
}
public void beginTask ()
{
isPaused = false;
isStopped = false;
stopwatch.Restart();
}
public void pauseTask ()
{
if (!isPaused && !isStopped)
{
isPaused = true;
isFormUpdatedAfterPause = false;
stopwatch.Stop();
}
}
public void resumeTask ()
{
if (isPaused && !isStopped)
{
isPaused = false;
stopwatch.Start();
}
}
public void stopTask ()
{
if (!isStopped)
{
isStopped = true;
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)
{
if (controller.isStopped)
return null;
while (controller.isPaused)
{
Thread.Sleep(100);
if (!controller.isFormUpdatedAfterPause)
{
controller.isFormUpdatedAfterPause = true;
_report.progress = counter;
_report.currentFileUrl = item;
_report.currentTreeNode = (TreeNode)itemsNode.Clone();
report.Report(_report);
}
}
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, ""));
}
}
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;
}
}
}