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

99 lines
3.2 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;
namespace fileFinder
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
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;
}
}
}
bool hintHided = false;
private void curDirTextBox_Enter(object sender, EventArgs e)
{
if (!hintHided)
{
hintHided = true;
curDirTextBox.Text = "";
}
}
private void handleSearchBtn_Click(object sender, EventArgs e)
{
resultViewer.Nodes.Clear();
List<String> foundFiles = new List<String>();
TreeNode itemsNode = new TreeNode();
try
{
foundFiles = Directory.GetFiles(curDirTextBox.Text, queryTextBox.Text, SearchOption.AllDirectories).ToList<String>();
} catch { }
foreach (String item in foundFiles)
fillChildNode(itemsNode, item.Replace(curDirTextBox.Text, ""));
resultViewer.Nodes.Add(itemsNode);
}
public void fillChildNode (TreeNode node, String item)
{
int backSlashIndex = item.IndexOf("\\");
if (backSlashIndex > -1)
{
if (backSlashIndex == 0)
{
item = item.Remove(0, 1);
backSlashIndex = item.IndexOf("\\");
}
String currentNodeName = item.Substring(0, backSlashIndex);
int nodeIndex = node.Nodes.IndexOfKey(currentNodeName);
if (nodeIndex != -1)
fillChildNode(node.Nodes[nodeIndex], item.Remove(0, backSlashIndex + 1));
else
{
node.Nodes.Add(currentNodeName, currentNodeName, 0, 0);
nodeIndex = node.Nodes.IndexOfKey(currentNodeName);
fillChildNode(node.Nodes[nodeIndex], item.Remove(0, backSlashIndex + 1));
}
} else
node.Nodes.Add(item, item, 2, 2);
}
}
}