NEWS

[C#] Hướng dẫn Khóa và mở khóa Folder

[C#] Hướng dẫn Khóa và mở khóa Folder
Đăng bởi: Thảo Meo - Lượt xem: 6107 12:02:24, 15/08/2021C#   In bài viết

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ác khóa và mở khóa folder trong lập trình C#, Winform.

[C#] Lock and Unlock folder winform

Dưới đây là hình ảnh demo ứng dụng:

lock_folder_winform

Các bạn có thể xem video demo ứng dụng:

Cách thực hiện khóa và mở khóa bài viết này cũng đơn giản:

Nếu các bạn thực hiện bằng tay trên Windows thì sẽ thực hiện như hình bên dưới.

Hình ảnh dưới đây là sẽ set Permission Deny truy cập thư mục Folder.

lock_folder_windows

Nếu các bạn xóa dòng Deny ở hình trên ra thì các bạn có thể truy cập thư mục lại bình thường.

Full source code Lock Folder c#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LockFolderCsharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            var folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnUnlck_Click(object sender, EventArgs e)
        {
            if (txtPath.Text.Length > 0)
            {
                try
                {
                    string folderPath = txtPath.Text;
                    string adminUserName = Environment.UserName;
                    DirectorySecurity ds = Directory.GetAccessControl(folderPath);
                    FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName, FileSystemRights.FullControl, AccessControlType.Deny);
                    ds.RemoveAccessRule(fsa);
                    Directory.SetAccessControl(folderPath, ds);
                    MessageBox.Show("Unlocked");
                }
                catch
                {
                }
            }
        }

        private void bntLock_Click(object sender, EventArgs e)
        {
            if (txtPath.Text.Length > 0)
            {
                try
                {
                    string folderPath = txtPath.Text;
                    string adminUserName = Environment.UserName;
                    DirectorySecurity ds = Directory.GetAccessControl(folderPath);
                    FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName, FileSystemRights.FullControl, AccessControlType.Deny);
                    ds.AddAccessRule(fsa);
                    Directory.SetAccessControl(folderPath, ds);
                    MessageBox.Show("Locked");
                }

                catch
                {
                }
            }
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn Khóa và mở khóa Folder
Đăng bởi: Thảo Meo - Lượt xem: 6107 12:02:24, 15/08/2021C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.