概述
一个小工具,功能有启用禁用外网、USB接口,可由服务端socket长链接进行操控客户端从而达到实现前边的这些功能,这里贴上核心代码,先给上启用禁用USB接口吧,这个方法可随时启用禁用,之前用过一个改usb接口状态那个方法不大行,对于已经插进去的没啥作用了,这个是后来改过的
启用USB
public bool EnableUSB() { if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices")) { CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindows", "RemovableStorageDevices", ""); } if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}")) { CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices", "{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", ""); } SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Read", "0", "REG_DWORD"); SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Write", "0", "REG_DWORD"); bool blDevices = SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_All", "0", "REG_DWORD"); if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SYSTEMCurrentControlSetservicesUSBSTOR")) { CreateRegistKey("HKEY_LOCAL_MACHINE", @"SYSTEMCurrentControlSetservices", "USBSTOR", ""); } bool blUsbStor = SetRegistValue("HKEY_LOCAL_MACHINE", @"SYSTEMCurrentControlSetservicesUSBSTOR", "Start", "3", "REG_DWORD"); if (GetSystemIs64() == "64") { if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices")) { CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindows", "RemovableStorageDevices", ""); } if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}")) { CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices", "{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", ""); } SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Read", "0", "REG_DWORD"); SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Write", "0", "REG_DWORD"); SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_All", "0", "REG_DWORD"); } return blDevices && blUsbStor; }
禁用USB
public bool DisableUSB() { if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices")) { CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindows", "RemovableStorageDevices", ""); } if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}")) { CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices", "{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", ""); } SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Read", "1", "REG_DWORD"); SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Write", "1", "REG_DWORD"); bool blDevices = SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREPoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_All", "1", "REG_DWORD"); if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SYSTEMCurrentControlSetservicesUSBSTOR")) { CreateRegistKey("HKEY_LOCAL_MACHINE", @"SYSTEMCurrentControlSetservices", "USBSTOR", ""); } bool blUsbStor = SetRegistValue("HKEY_LOCAL_MACHINE", @"SYSTEMCurrentControlSetservicesUSBSTOR", "Start", "4", "REG_DWORD"); if (GetSystemIs64() == "64") { if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices")) { CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindows", "RemovableStorageDevices", ""); } if (!IsExistRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}")) { CreateRegistKey("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices", "{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", ""); } SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Read", "1", "REG_DWORD"); SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_Write", "1", "REG_DWORD"); SetRegistValue("HKEY_LOCAL_MACHINE", @"SOFTWAREWow6432NodePoliciesMicrosoftWindowsRemovableStorageDevices{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", "Deny_All", "1", "REG_DWORD"); } return blDevices && blUsbStor; }
判断获取操作系统位数
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 ""; } }
操作注册表
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; } }
以上内容都是比较固定死的,没什么技术含量,这些内容可以直接拿着作为helper用了
(下一个是启用禁用外网)
转载于:https://www.cnblogs.com/sixthrhapsody/p/5773230.html
最后
以上就是秀丽胡萝卜为你收集整理的启用禁用USB接口的全部内容,希望文章能够帮你解决启用禁用USB接口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复