- [C#] Hướng dẫn viết ứng dụng theo dõi máy in bao nhiêu trang (Monitor Printer)
- [C#] Lấy thông tin cấu hình máy tính xuất ra text file winform
- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
[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!