- Tạo bản quyền phần mềm C# và bảo mật code | The Enigma Protector
- [C#] Hướng dẫn giới hạn số cửa sổ ứng dụng khi chạy trên winform
- [C#] Lập trình ứng dụng lấy ngày giờ hệ thống mạng LAN sử dụng giao thức UDP
- [DATABASE] Sự khác nhau giữa hai câu lệnh TRUNCATE vs DELETE trong sqlserver
- [C#] Các cách chuyển đổi kiểu dữ liệu text String sang kiểu số Int
- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
[C#] Khóa, mở khóa, protected file readonly USB trên winform
Xin chào các bạn, bài viết hôm nay mình sẽ 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:
- Khóa cổng kết nối USB
- Mở cổng kết nối USB
- Cho phép chỉ đọc dữ liệu trên USB, không cho phép ghi dữ liệu vào
- 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.
Giao diện demo ứng USB Management C#:
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";
}
}
Hy vọng bài viết sẽ giúp ích được cho các bạn.
Thanks for watching!