你的位置:首页 > ASP.net教程

[ASP.net教程]启用禁用USB接口


一个小工具,功能有启用禁用外网、USB接口,可由服务端socket长链接进行操控客户端从而达到实现前边的这些功能,这里贴上核心代码,先给上启用禁用USB接口吧,这个方法可随时启用禁用,之前用过一个改usb接口状态那个方法不大行,对于已经插进去的没啥作用了,这个是后来改过的

启用USB

    public bool EnableUSB()    {      if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices"))      {        CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows", "RemovableStorageDevices", "");      }      if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}"))      {        CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices", "{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "");      }      SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Read", "0", "REG_DWORD");      SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Write", "0", "REG_DWORD");      bool blDevices = SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_All", "0", "REG_DWORD");      if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SYSTEM\CurrentControlSet\services\USBSTOR"))      {        CreateRegistKey("HKEY_LOCAL_MACHINE", @"SYSTEM\CurrentControlSet\services\", "USBSTOR", "");      }      bool blUsbStor = SetRegistValue("HKEY_LOCAL_MACHINE", @"SYSTEM\CurrentControlSet\services\USBSTOR", "Start", "3", "REG_DWORD");      if (GetSystemIs64() == "64")      {        if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices"))        {          CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\", "RemovableStorageDevices", "");        }        if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}"))        {          CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices\", "{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "");        }        SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Read", "0", "REG_DWORD");        SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Write", "0", "REG_DWORD");        SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_All", "0", "REG_DWORD");      }      return blDevices && blUsbStor;    }

View Code

 禁用USB

    public bool DisableUSB()    {      if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices"))      {        CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows", "RemovableStorageDevices", "");      }      if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}"))      {        CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices", "{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "");      }      SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Read", "1", "REG_DWORD");      SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Write", "1", "REG_DWORD");      bool blDevices = SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_All", "1", "REG_DWORD");      if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SYSTEM\CurrentControlSet\services\USBSTOR"))      {        CreateRegistKey("HKEY_LOCAL_MACHINE", @"SYSTEM\CurrentControlSet\services\", "USBSTOR", "");      }      bool blUsbStor = SetRegistValue("HKEY_LOCAL_MACHINE", @"SYSTEM\CurrentControlSet\services\USBSTOR", "Start", "4", "REG_DWORD");      if (GetSystemIs64() == "64")      {        if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices"))        {          CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\", "RemovableStorageDevices", "");        }        if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}"))        {          CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices\", "{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "");        }        SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Read", "1", "REG_DWORD");        SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Write", "1", "REG_DWORD");        SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_All", "1", "REG_DWORD");      }      return blDevices && blUsbStor;    } 

View Code

判断获取操作系统位数

    private string GetSystemIs64()    {      try      {        string systemtype = "";        ObjectQuery oq = new ObjectQuery("select AddressWidth from Win32_Processor");        ManagementObjectSearcher query1 = new ManagementObjectSearcher(oq);        ManagementObjectCollection moc = query1.Get();        foreach (ManagementObject mo in moc)        {          try          {            systemtype = mo["addressWidth"].ToString();          }          catch          {            systemtype += "null";            continue;          }        }        moc = null;        oq = null;        query1 = null;        return systemtype;      }      catch (Exception ex)      {        ErrorCollectHelper.ErrorLog("获取操作系统位数错误", ex.ToString());        return "";      }    } 

View Code

操作注册表

    private bool IsExistRegistKey(string rootKey, string subKey)    {      bool isExistKey = false;      try      {        RegistryKey hkml = GetRegistryKey(rootKey);        if (hkml != null)        {          RegistryKey subkey = hkml.OpenSubKey(subKey, true);          if (subkey != null)            isExistKey = true;        }      }      catch (Exception ex)      {        ErrorCollectHelper.ErrorLog("打开注册表错误", ex.ToString());      }      return isExistKey;    }    private void CreateRegistKey(string rootKey, string subKey, string keyName, string keyValue)    {      try      {        RegistryKey hkml = GetRegistryKey(rootKey);        if (hkml != null)        {          RegistryKey subkey = hkml.CreateSubKey(subKey);          if (subkey != null && keyName != "")            subkey.SetValue(keyName, keyValue, RegistryValueKind.String);        }      }      catch (Exception ex)      {        ErrorCollectHelper.ErrorLog("创建注册表错误", ex.ToString());      }    }    private bool SetRegistValue(string rootKey, string subKey, string key, string value, string type)    {      try      {        RegistryKey hkml = GetRegistryKey(rootKey);        if (hkml != null)        {          RegistryKey secKey = hkml.OpenSubKey(subKey, true);          if (secKey != null)            secKey.SetValue(key, value, GetValueKind(type));          else          {            secKey = hkml.CreateSubKey(subKey);            if (secKey != null)              secKey.SetValue(key, value, GetValueKind(type));          }        }        return true;      }      catch (Exception ex)      {        ErrorCollectHelper.ErrorLog("修改注册表错误", ex.ToString());        return false;      }    }    private bool DeleteRegistValue(string rootKey, string subKey, string key)    {      try      {        RegistryKey hkml = GetRegistryKey(rootKey);        if (hkml != null)        {          RegistryKey secKey = hkml.OpenSubKey(subKey, true);          if (secKey != null)          {            if (secKey.GetValue(key) != null)              secKey.DeleteValue(key);          }        }        return true;      }      catch (System.Exception ex)      {        ErrorCollectHelper.ErrorLog("删除注册表错误", ex.ToString());        return false;      }    }    private RegistryValueKind GetValueKind(string typeInfo)    {      switch (typeInfo)      {        case "REG_DWORD":          return RegistryValueKind.DWord;        case "REG_SZ":          return RegistryValueKind.String;        default:          return RegistryValueKind.None;      }    }    private RegistryKey GetRegistryKey(string rootkey)    {      if (string.IsNullOrEmpty(rootkey))        return null;      switch (rootkey)      {        case "HKEY_CURRENT_USER":          return Registry.CurrentUser;        case "HKEY_LOCAL_MACHINE":          return Registry.LocalMachine;        case "HKEY_CLASSES_ROOT":          return Registry.ClassesRoot;        case "HKEY_USERS":          return Registry.Users;        case "HKEY_CURRENT_CONFIG":          return Registry.CurrentConfig;        default:          return null;      }    }

View Code

以上内容都是比较固定死的,没什么技术含量,这些内容可以直接拿着作为helper用了