- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
[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!