2020年12月1日 星期二

C# 鎖USB

 https://blog.miniasp.com/post/2008/01/18/How-to-disable-the-use-of-USB-storage-devices

https://blog.xuite.net/kenny.pro2/blog/212310907

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

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

        private void button1_Click(object sender, EventArgs e)  //"禁用USB隨身碟"那個Button
        {
            if (textBox1.Text == "1qaz")  //每次都要先輸入此密碼 ,Button才能按
            {
                if (MessageBox.Show("是否確定要禁用隨身碟!!", "禁用!!", MessageBoxButtons.YesNo) == DialogResult.Yes)   // YES/NO 確認
                {
                    Reg_Method(@" ADD HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesUSBSTOR /v Start /t REG_DWORD /d 4 /f");
                    MessageBox.Show("已禁用隨身碟 !!   隨身碟重拔插後生效");
                } 
            }
            else
                MessageBox.Show("密碼錯誤");
        }

        private void button2_Click(object sender, EventArgs e)  //"開放USB隨身碟"那個Button
        {
            if (textBox1.Text == "1qaz")
            {
                if (MessageBox.Show("是否確定要開放隨身碟!!", "開放!!", MessageBoxButtons.YesNo) == DialogResult.Yes)    // YES/NO 確認
                {
                    Reg_Method(@" ADD HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesUSBSTOR /v Start /t REG_DWORD /d 3 /f");
                    MessageBox.Show("已開放隨身碟 !!   隨身碟重拔插後生效");
                }
            }
            else
                MessageBox.Show("密碼錯誤");
        }

        private void button3_Click(object sender, EventArgs e)  //"唯讀USB隨身碟"那個Button
        {
            if (textBox1.Text == "1qaz")
            {
                if (MessageBox.Show("是否確定要"唯讀"USB隨身碟!!", "唯讀!!", MessageBoxButtons.YesNo) == DialogResult.Yes)   // YES/NO 確認
                {
                    Reg_Method(@" ADD HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlStorageDevicePolicies /v WriteProtect /t REG_DWORD /d 1 /f");
                    MessageBox.Show("已唯讀隨身碟 !!   隨身碟重拔插後生效");
                }
            }
            else
                MessageBox.Show("密碼錯誤");
        }

        private void button4_Click(object sender, EventArgs e)  //"可寫入USB隨身碟"那個Button
        {
            if (textBox1.Text == "1qaz")
            {
                if (MessageBox.Show("是否確定要讓隨身碟可寫入!!", "可寫入!!", MessageBoxButtons.YesNo) == DialogResult.Yes)   // YES/NO 確認
                {
                    Reg_Method(@" ADD HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlStorageDevicePolicies /v WriteProtect /t REG_DWORD /d 0 /f");
                    MessageBox.Show("隨身碟已可寫入 !!   隨身碟重拔插後生效");
                }
            }
            else
                MessageBox.Show("密碼錯誤");
        }

        private void button5_Click(object sender, EventArgs e)  //說明那個Button
        {
            MessageBox.Show("禁用USB隨身碟 : 禁用所有Windows認定的USB儲存裝置"
                + Environment.NewLine + "開放USB隨身碟 : 開放所有Windows認定的USB儲存裝置"
                + Environment.NewLine + "唯讀USB隨身碟 : USB儲存裝置,只能讀取無法寫入(限此台電腦)"
                + Environment.NewLine + "可寫入USB隨身碟 : USB儲存裝置,只能讀取可讀可寫(限此台電腦)");
        }

        // --------------------------------  Register Method  ---------------------------------------------------
        private static void Reg_Method(string x)
        {
            ProcessStartInfo reg = new ProcessStartInfo("reg.exe", x);
            reg.WindowStyle = ProcessWindowStyle.Hidden;
            Process.Start(reg);
        }
    }
}

執行起來Form的樣子:



沒有留言:

張貼留言

WPF聊天室应用(ASP.NET Core SignalR)

  WPF聊天室应用(ASP.NET Core SignalR) https://www.bilibili.com/video/BV1Q741187Si?p=2 https://www.bilibili.com/video/BV1UV411e75T?from=search&...