BomKhung.Com Đã Quay Trở Lại
Hoạt Động Dưới Tên Miền Hung.Pro.VN
00 Days
00 Hours
00 Minutes
00 Seconds
Hiện website đang cập nhật nội dung bài viết, nếu có lỗi gì mọi người có thể thông báo cho mình Tại đây!

[C#] How to Anti Remove File in Winform

Chào các bạn, hôm nay mình sẽ hướng dẫn các bạn viết chương trình khoá không cho xoá file từ Process ID

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.

Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin

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.

FULL CODE ANTI REMOVE FILE

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();
        }
    }
}

Chúc các bạn thành công với thủ thuật này, và các bạn có thể thêm vào chương trình của mình để khoá không cho người dùng REMVOE APP của mình trên hệ thống của người dùng nhé.
 
Và điều đó sẽ gây ra phiền hà cho người dùng nên mọi người lưu ý nhé.

DOWNLOAD SOURCE CODE


PASSWORD UNZIP: HUNG.PRO.VN

Đăng nhận xét

Đồng ý sữ dụng cookie
Chúng tôi sử dụng cookie trên trang web này để phân tích lưu lượng truy cập, ghi nhớ tùy chọn của bạn và tối ưu hóa trải nghiệm của bạn.
Xem thêm
Oops!
Có vẻ như kết nối internet của bạn có vấn đề. Vui lòng kết nối lại và duyệt web.
AdBlock Detected!
Chúng tôi phát hiện bạn đang sử dụng plugin chặn quảng cáo trong trình duyệt của mình.
Doanh thu chúng tôi kiếm được từ quảng cáo được sử dụng để quản lý trang web này, chúng tôi yêu cầu bạn đưa trang web của chúng tôi vào danh sách trắng trong plugin chặn quảng cáo của bạn.
Site is Blocked
Sorry! This site is not available in your country.
Kỹ thuật số thế hệ tiếp theo Chào mừng bạn đến với trò chuyện WhatsApp
Xin chào! Chúng tôi có thể giúp gì cho bạn hôm nay?
Nhập vào đây...