- [C#] Hướng dẫn giải nén file *.rar với tiến trình progress bar winform
- [C#] Chia sẻ source code make Crazy Error Message Effect Bomb Windows
- [C#] Lập trình ứng dụng theo mô hình MVP Model-View-Presenter Pattern Winform
- [C#] Giới thiệu và những thứ hay ho từ thư viện System.Reactive của Microsoft
- [C#] Hướng dẫn tạo ứng dụng Chat với GPT sử dụng Open AI API
- [DEVEXPRESS] Tạo month picker trên DateEdit Winform C#
- [DATABASE] Cách sử dụng và lưu ý khi sử dụng khóa ngoại (Foreign Key) trong Sqlserver
- [C#] Garbage Collector (GC) là gì? Cách giải phóng bộ nhớ trên ứng dụng Winform khi các đối tượng không còn sử dụng
- [C#] Cách tính độ tương phản màu sắc Contrast Color mà con người có thể nhìn thấy được
- [C#] Hướng dẫn mã hóa mật khẩu tài khoản ứng dụng đúng chuẩn Men
- [C#] Sử dụng Open AI Chat GPT viết ứng dụng Count down timer có hiệu ứng trên winform
- [DATABASE] Chia sẻ dữ liệu Pantone Color sql và json api
- [SQLSERVER] Tạo mã sản phẩm tự động như: SP0001, SP0002, SP0003... sử dụng Trigger
- [C#] Hướng dẫn kiểm tra phiên bản NET Framework cài đặt ở máy tính
- [C#] Hướng dẫn đọc file excel đơn giản sử dụng thư viện Epplus
- [C#] ConcurrentBag là gì và cách sử dụng nó trong lập trình bất đồng bộ
- [C#] AutoResetEvent là gì và cách sử dụng
- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
[C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách tìm kiếm tập tin File nhanh chóng sử dụng thư viện FastSearchLibrary trong lập trình C#, Winform.
[C#] Search File Fast Winform
Dưới đây là giao diện demo ứng dụng tìm kiếm tập tin File:
Các thực hiện, các bạn truyền vào thư mục để tìm kiếm file, và một danh sách file cần tìm (có thể tìm nhiều file cùng một lúc).
Lưu ý: Ứng dụng này, các bạn có thể dùng để viết dịch vụ Services tìm kiếm (không nên tìm kiếm nguyên ổ đĩa có thể bị treo...)
Đầu tiên, các bạn cần import thư viện SearchFastLibrary.dll
thì source code của mình vào project của các bạn.
Sau khi import vào khi nào kiếm file sẽ có các sự kiện sau, cho các bạn quản lý để xuất thông báo cho người dùng.
1. Event Searcher_FilesFound
=> khi tìm thấy file thỏa điều kiện thì xuất ra kết quả
2. Event Searcher_SearchCompleted
=> Task thực thi tìm kiếm hoàn tất và thông báo số lượng file tìm thấy và thời gian thực hiện.
Full source code Fast Search File C#:
using FastSearchLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SearchFileFast
{
public partial class Form1 : Form
{
private object locker = new object();
private List<FileInfo> files;
private Stopwatch stopWatch;
public Form1()
{
InitializeComponent();
}
public async Task StartSearch(string PathFolderSearch, List<string> list_file, DateTime _datetime)
{
stopWatch = new Stopwatch();
stopWatch.Start();
WriteToFile("===========================================================\n", _datetime);
WriteToFile("APP is started at " + DateTime.Now, _datetime);
WriteToFile($"===> Folder Search: {PathFolderSearch}\n", _datetime);
int i = 0;
foreach (var file in list_file)
{
i++;
WriteToFile($"Searching File {i}.{file}\n", _datetime);
}
WriteToFile("===========================================================\n", _datetime);
files = new List<FileInfo>();
List<string> searchDirectories = new List<string>
{
PathFolderSearch
};
FileSearcherMultiple searcher = new FileSearcherMultiple(searchDirectories, (f) =>
{
foreach (var file in list_file)
{
if ( f.FullName.ToLower().Contains(file.ToLower()))
{
return true;
}
}
return false;
}, new CancellationTokenSource());
searcher.FilesFound += (s, e) => { Searcher_FilesFound(s, e, _datetime); };
searcher.SearchCompleted += (s, e) => { Searcher_SearchCompleted(s, e, _datetime); };
try
{
await searcher.StartSearchAsync();
}
catch (AggregateException ex)
{
WriteToFile($"Error occurred: {ex.InnerException.Message}", _datetime);
}
catch (Exception ex)
{
WriteToFile($"Error occurred: {ex.Message}", _datetime);
}
finally
{
WriteToFile("\nFinish!", _datetime);
}
}
public void WriteToFile(string Message, DateTime dateTime)
{
rtbResult.BeginInvoke(new Action(() => {
rtbResult.AppendText(Message);
}));
}
private void Searcher_FilesFound(object sender, FileEventArgs arg, DateTime _datetime)
{
lock (locker)
{
arg.Files.ForEach((f) =>
{
files.Add(f);
WriteToFile($"File location: {f.FullName}\nCreation.Time: {f.CreationTime}\n", _datetime);
});
}
}
private void Searcher_SearchCompleted(object sender, SearchCompletedEventArgs arg, DateTime _datetime)
{
stopWatch.Stop();
if (arg.IsCanceled)
WriteToFile("Search stopped.", _datetime);
else
{
lbl_Result.BeginInvoke(new Action(() =>
{
lbl_Result.Text += "Search completed.";
}));
}
lbl_Result.BeginInvoke(new Action(() =>
{
lbl_Result.Text += $" Quantity of files: {files.Count}";
lbl_Result.Text += $" Spent time: {stopWatch.Elapsed.Minutes} min {stopWatch.Elapsed.Seconds} s {stopWatch.Elapsed.Milliseconds} ms ";
}));
}
private void btnBrowse_Click(object sender, EventArgs e)
{
var ofd = new FolderBrowserDialog();
if(ofd.ShowDialog() == DialogResult.OK)
{
txtFolderSearch.Text = ofd.SelectedPath;
}
}
private async void btnSearch_Click(object sender, EventArgs e)
{
lbl_Result.Text = "";
rtbResult.Text = "";
string folderSearch = txtFolderSearch.Text;
string[] stringSeparators = new string[] { "\r\n" };
string[] lines = txtListFile.Text.Split(stringSeparators, StringSplitOptions.None);
await StartSearch(folderSearch, lines.ToList(), DateTime.Now);
}
}
}
Thanks for watching!