用C#来做一个禁止U盘的简单程序

  其实用到的原理很简单,就是来简单的对window系统内的注册表进行修改。

win+r,输入regedit,进入注册表编辑器。找到HEKY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\service\USBSTOR里面,找到名称为start的这个选项,讲它的值更改为4,就是禁止。2就是自动,3是手动。

程序思路就是这样,通过修改这个注册表的值来起到禁用U盘或者取消禁用U盘的功能。

先创建一个项目,引入命名空间using Microsoft.Win32;  该命名空间中有RegisttryKey这个类,里面还有要用到的OpenSubKey方法(打开注册表)、GetValue方法(获取注册表值),SetValue方法(写入注册表值)。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;


namespace 读取注册表的值
{
    class Program
    {
        static void Main(string[] args)
        {
              RegistryKey key = Registry.LocalMachine;
            RegistryKey software = key.OpenSubKey(@"SYSTEM\CurrentControlSet\services\USBSTOR",true);   //true表示有写的权限
            string GetValue = software.GetValue("Start").ToString();
            Console.WriteLine(GetValue);
            software.SetValue("Start",4);
            Console.WriteLine(GetValue);
            Console.ReadKey();

         }

     }

然后再写个winform画面,优化一下就好。

猜你喜欢

转载自blog.csdn.net/sinat_27998085/article/details/80545190