Notification texts go here Contact Us Buy Now!

[C#] Lock, unlock, protected file readonly USB on winform

.Net ,C# Lock, unlock, protected file readonly USB on winform,
Hôm nay mình sẽ chia sẽ cho mọi người một thủ thuật đơn giản để bảo vệ máy tính cá nhân nếu có người cắm USB vào máy tính của bạn để trộm thông tin hay làm gì máy tính của bạn nhé.
 
Ở bài viết này mình hướng dẫn các bạn cách quản lý port usb trên windows bằng ngôn ngữ lập trình C#.

[C#] Khóa, mở khóa và protected Write file trên USB Winform


Bài này, mình chưa sẽ làm việc với thiết bị USB, bao gồm 4 chức năng:
  1. Khóa cổng kết nối USB
  2. Mở cổng kết nối USB
  3. Cho phép chỉ đọc dữ liệu trên USB, không cho phép ghi dữ liệu vào
  4. Cho phép đọc và ghi dữ liệu trên USB.
Sau khi, accept từng chức năng, các bạn cần tháo usb ra và gắn lại để test thử.
Các ứng dụng, này thường được sử dụng trong một số công ty, một số công ty nhằm bảo mật không cho người dùng, sao chép dữ liệu công ty về máy tính cá nhân.

Hoặc có thể ngăn ngừa máy tính bị nhiễm Virus từ USB cá nhân vào hệ thống công ty, v.v...

Trong bài viết, chúng ta muốn lock port USB thì phải thông qua Regedit trong Winform, vì thế, ứng dụng của chúng ta cần phải chạy ở chế độ Run As Administrator để thực hiện.

Để ứng dụng, chạy chế độ Admin, các bạn có thể đọc bài viết trước mình đã hướng dẫn.

Source code Full Procted USB C#:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UsbManagement
{
    public partial class Form1 : Form
    {
        RegistryKey Regkey, RegKey2;
        Int32 rValue, rsvalue, Gvalue, tvalue;
        bool isAdmin;
        [DllImport("shell32")]
        static extern bool IsUserAnAdmin();
        public Form1()
        {
            InitializeComponent();
        }


        private void RadioButtonenable_CheckedChanged(object sender, EventArgs e)
        {
            groupBox2.Enabled = true;
            rValue = 3;
        }

        private void RadioButtonreadonly_CheckedChanged(object sender, EventArgs e)
        {
            rsvalue = 1;
        }

       
        private void RadioButtonreadwrite_CheckedChanged(object sender, EventArgs e)
        {
            rsvalue = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            isAdmin = IsUserAnAdmin();
            if (isAdmin == false)
            {
                MessageBox.Show("You don't have proper privileges level to make changes, administrators privileges are required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            else
            {
                Regkey = Registry.LocalMachine.OpenSubKey(Regpath, true);
                Gvalue = Convert.ToInt32(Regkey.GetValue("Start"));
                //check the current state of the usb/whether is enabled or disabled
                if (Gvalue == 3)
                {
                    RadioButtonenable.Checked = true;
                }
                else if (Gvalue == 4)
                {
                    RadioButtondisable.Checked = true;
                }
                RegKey2 = Registry.LocalMachine.OpenSubKey(ReadAndWriteRegPath, true);
                try
                {
                    tvalue = Convert.ToInt32(RegKey2.GetValue("WriteProtect"));
                    if (tvalue == 1)
                    {
                        RadioButtonreadonly.Checked = true;
                    }
                    else if (tvalue == 0)
                    {
                        RadioButtonreadwrite.Checked = true;
                    }
                }
                catch (NullReferenceException) { }
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            Regkey = Registry.LocalMachine.OpenSubKey(Regpath, true);
            Regkey.SetValue("Start", rValue);
            if (groupBox2.Enabled == true)
            {
                RegKey2 = Registry.LocalMachine.OpenSubKey(ReadAndWriteRegPath2, true);
                RegKey2.CreateSubKey("StorageDevicePolicies");
                RegKey2 = Registry.LocalMachine.OpenSubKey(ReadAndWriteRegPath, true);
                RegKey2.SetValue("WriteProtect", rsvalue);
            }
            if ((rValue == 3) && (rsvalue == 1))
            {
                MessageBox.Show("USB Port were enable and Read only is enabled");
            }
            else if ((rValue == 3) && (rsvalue == 0))
            {
                MessageBox.Show("USB Port were enable and Read and write is enabled");
            }
            else
            {
                MessageBox.Show("USB Port were disable");
            }
        }

        private void RadioButtondisable_CheckedChanged(object sender, EventArgs e)
        {
            groupBox2.Enabled = false;
            rValue = 4;
        }

        string Regpath = "System\\CurrentControlSet\\Services\\USBSTOR";
        string ReadAndWriteRegPath2 = "System\\CurrentControlSet\\Control";
        string ReadAndWriteRegPath = "System\\CurrentControlSet\\Control\\StorageDevicePolicies";
    }
}

DOWNLOAD SOURCE CODE
DOWNLOAD NOW
Chúc mọi người thành công và bảo về các USB lạ muốn kết nối với máy tính của các bạn.
Theo LapTrinhVB.Net

About the Author

Chia sẽ kiến thức và tư duy sáng tạo của bản thân mỗi ngày cho cộng đồng IT Việt Nam và Thế giới. Chia sẽ thủ thuật lập trình Csharp, Visual Basic, Python, Php, Javascript, Css, Html. Tối ưu hóa Windows, Microsoft, Facebook, Youtube, Chrome...v.v. …

تعليقان (2)

  1. Cắm usb vào cũng sợ bị dính virus r. Chứ huống gì là khoá haha
  2. như thế nào cũng k bảo mật tốt đâu e, tùy thuộc vào ng` dùng hệ thống thôi
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.
NextGen Digital Welcome to WhatsApp chat
Howdy! How can we help you today?
Type here...