[CSHARP] 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.
[CSHARP] Lock, unlock, protected file readonly USB on winform

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

PASSWORD UNZIP: HUNG.PRO.VN
Chúc Mọi Người Thành Công Với Thủ Thuật Trên.
Nếu mọi người có vướng mắc gì mình chia sẽ trên trang có thể gửi liên hê cho mình tại đây nhé.
Cảm ơn mọi người đã quan tâm.

No comments:

All Right Reserved © 2015 By Hung Pro VN