- [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
Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
Xin chào các bạn, bài viết hôm nay mình chia sẻ các bạn source code, Anti Remove File tập tin trên lập trình C#, winform.
[C#] How to Anti Remove File in Winform
Khi các bạn chạy ứng dụng, ứng dụng file sẽ được chèn vào nhiều process.
Nên khi bạn muốn xóa tập tin này sẽ không được, vì thông báo tập tin này đang được process nào đó sử dụng.
Hình ảnh minh họa bên dưới:
Bây giờ, khi các bạn xóa tập tin file đều không được, các bạn chỉ có thể xóa khi logout hoặc retart lại máy tính.
Source code c#:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
static class AntiRemove
{
const uint GENERIC_READ = 0x80000000;
const uint DUPLICATE_SAME_ACCESS = 0x00000002;
const uint FILE_SHARE_READ = 1;
const uint OPEN_EXISTING = 3;
public static bool LockFile(string filePath, params string[] processes)
{
IntPtr hCurrentProcess;
using (Process current = Process.GetCurrentProcess())
{
hCurrentProcess = current.Handle;
}
IntPtr hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (hFile == IntPtr.Zero)
return false;
int count = 0;
foreach (Process p in Process.GetProcesses())
{
using (p)
{
try
{
if (processes.Length == 0 || Array.IndexOf(processes, p.ProcessName.ToLowerInvariant()) != -1)
{
Console.WriteLine("Locking with " + p);
IntPtr hFileClone;
IntPtr hProcess = p.Handle;
if (!DuplicateHandle(((IntPtr)(-1)), hFile, hProcess, out hFileClone, GENERIC_READ, false, DUPLICATE_SAME_ACCESS))
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
count++;
}
}
catch
{
Console.WriteLine("Failed locking with " + p);
}
}
}
CloseHandle(hFile);
return count > 0;
}
public static bool UnlockFile(string filePath)
{
IntPtr hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (hFile == IntPtr.Zero)
return false;
if (!CloseHandle(hFile))
{
Console.WriteLine("Failed to unlock the file.");
return false;
}
Console.WriteLine("File unlocked successfully.");
return true;
}
[DllImport("kernel32", SetLastError = true)]
static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32", SetLastError = true)]
static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, IntPtr hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle, uint dwDesiredAccess, bool bInheritHandle, uint dwOptions);
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);
}
namespace ConsoleApp10
{
internal class Program
{
static void Main(string[] args)
{
string filePath = "product_id.png";
bool locked = AntiRemove.LockFile(filePath);
if (locked)
{
Console.WriteLine("File locked successfully.");
}
else
{
Console.WriteLine("Failed to lock the file.");
}
Console.ReadLine();
}
}
}
Thanks for watching!