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.
221 lines
7.8 KiB
221 lines
7.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Timers;
|
|
|
|
namespace fileFinder
|
|
{
|
|
public partial class MainForm : Form
|
|
{
|
|
bool hintHided = false;
|
|
TaskController mainController;
|
|
System.Timers.Timer updateInfoTimer = new System.Timers.Timer(1000);
|
|
|
|
public MainForm()
|
|
{
|
|
InitializeComponent();
|
|
mainController = new TaskController();
|
|
this.updateInfoTimer.Elapsed += refreshInfoByTimer;
|
|
this.updateInfoTimer.AutoReset = true;
|
|
this.updateInfoTimer.Enabled = true;
|
|
try
|
|
{
|
|
ImageList iconList = new ImageList();
|
|
iconList.Images.Add(Image.FromFile("Folder.png")); // 0
|
|
iconList.Images.Add(Image.FromFile("OpenedFolder.png")); // 1
|
|
iconList.Images.Add(Image.FromFile("File.png")); // 2
|
|
iconList.Images.Add(Image.FromFile("Search.png")); // 3
|
|
resultViewer.ImageList = iconList;
|
|
resultViewer.ImageIndex = 3;
|
|
resultViewer.SelectedImageIndex = 3;
|
|
} catch { }
|
|
|
|
}
|
|
|
|
private void dirSelectBtn_Click(object sender, EventArgs e)
|
|
{
|
|
using (var dialog = new FolderBrowserDialog())
|
|
{
|
|
DialogResult result = dialog.ShowDialog();
|
|
if (result == DialogResult.OK && !String.IsNullOrWhiteSpace(dialog.SelectedPath))
|
|
{
|
|
curDirTextBox.Text = dialog.SelectedPath;
|
|
hintHided = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void curDirTextBox_Enter(object sender, EventArgs e)
|
|
{
|
|
if (!hintHided)
|
|
{
|
|
hintHided = true;
|
|
curDirTextBox.Text = "";
|
|
}
|
|
}
|
|
|
|
private async Task<TreeNode> buildResultTree(TaskController controller, IProgress<ProgressReportModel> report)
|
|
{
|
|
TreeNode itemsNode = new TreeNode();
|
|
List<String> foundFiles = new List<String>();
|
|
ProgressReportModel _report = new ProgressReportModel();
|
|
|
|
try
|
|
{
|
|
foundFiles = Directory.GetFiles(curDirTextBox.Text, queryTextBox.Text, SearchOption.AllDirectories).ToList<String>();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Поиск не удался!");
|
|
}
|
|
|
|
int counter = 0;
|
|
foreach (String item in foundFiles)
|
|
{
|
|
while (controller.isPaused)
|
|
{
|
|
Thread.Sleep(100);
|
|
if (!mainController.isFormUpdatedAfterPause)
|
|
{
|
|
mainController.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, innerQueryTextBox.Text)))
|
|
{
|
|
counter++;
|
|
await fillChildNode(itemsNode, item.Replace(curDirTextBox.Text, ""));
|
|
}
|
|
}
|
|
controller.stopTask();
|
|
return itemsNode;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private void reportProgress(object sender, ProgressReportModel report)
|
|
{
|
|
if (!mainController.isPaused)
|
|
updateInfoLabel(report.currentFileUrl, report.progress);
|
|
else if (mainController.isPaused)
|
|
updateResultViewer(report.currentTreeNode);
|
|
}
|
|
|
|
private async void handleSearchBtn_Click(object sender, EventArgs e)
|
|
{
|
|
Progress<ProgressReportModel> progress = new Progress<ProgressReportModel>();
|
|
progress.ProgressChanged += reportProgress;
|
|
|
|
if (mainController.isStopped)
|
|
{
|
|
handleSearchBtn.Text = "Pause Task";
|
|
mainController.beginTask();
|
|
TreeNode tN = await Task.Run(() => buildResultTree(mainController, progress));
|
|
updateResultViewer(tN);
|
|
handleSearchBtn.Text = "Start Task";
|
|
} else if (mainController.isPaused)
|
|
{
|
|
handleSearchBtn.Text = "Pause Task";
|
|
mainController.resumeTask();
|
|
} else
|
|
{
|
|
handleSearchBtn.Text = "Resume Task";
|
|
mainController.pauseTask();
|
|
}
|
|
}
|
|
|
|
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 void updateInfoLabel (string fileUrl, int counter)
|
|
{
|
|
TimeSpan tS = mainController.elapsedTime();
|
|
string time = String.Format("{0:00}:{1:00}:{2:00}", tS.Hours, tS.Minutes, tS.Seconds);
|
|
infoLabel.Text = "Time: " + time + " " + "| Processed files: " + counter +
|
|
" | Current file: " + fileUrl;
|
|
|
|
}
|
|
|
|
public void updateResultViewer(TreeNode tN)
|
|
{
|
|
if (tN != null)
|
|
{
|
|
resultViewer.Nodes.Clear();
|
|
resultViewer.Nodes.Add(tN);
|
|
}
|
|
}
|
|
|
|
public void refreshInfoByTimer (object sender, ElapsedEventArgs e)
|
|
{
|
|
Action refresh = () =>
|
|
{
|
|
TimeSpan tS = mainController.elapsedTime();
|
|
string time = String.Format("{0:00}:{1:00}:{2:00}", tS.Hours, tS.Minutes, tS.Seconds);
|
|
infoLabel.Text = "Time: " + time + " |" +
|
|
infoLabel.Text.Substring(infoLabel.Text.IndexOf("|") + 1);
|
|
};
|
|
try { this.Invoke(refresh); } catch { }
|
|
}
|
|
}
|
|
}
|
|
|