启用禁止使用外网,启用禁止使用USB接口。紧接着上次的小工具启用禁止使用外网,也不是什么高技术含量的,拿来用就行了,所以那里就不多表达,代码里有简要介绍
跟着上次的小工具启用禁止使用外网,也不是何等高技术含量的,拿来用就行了,所以那里就不多表达,代码里有简要介绍
1个小工具,成效有启用禁止使用外网、USB接口,可由劳务端socket长链接举行操控客户端从而完结完毕前面包车型大巴那些效率,那里贴上着力代码,先给上启用禁止使用USB接口吧,这一个办法可每三十一日启用禁止使用,此前用过3个改usb接口状态相当情势十分小行,对于已经插进去的没啥功用了,这么些是后来改过的
贰个小工具,功效有启用禁止使用外网、USB接口,可由劳动端socket长链接举办操控客户端从而落成完毕前面包车型地铁那几个效应,那里贴上基本代码,先给上启用禁止使用USB接口吧,那几个方式可随时启用禁用,此前用过八个改usb接口状态不市场价格势非常小行,对于曾经插进去的没啥功用了,那么些是新兴改过的
启用禁止使用外网方法
启用禁止使用外网方法
启用USB
启用USB
public bool ForbiddenNetWork(bool isForbidden)
{
try
{
//禁用dns
if (isForbidden)
{
//如果双网卡:114.114.114.114|115.115.115.115|116.116.116.116 8.8.8.8
List<string[]> getDNSList = NetWorkSettingHelper.GetDNS();
if (getDNSList.Count <= 0)
{
VertifyPwd.isWlan = !setConfig.isForbiddenNetWork;
ErrorCollectHelper.InfoLog("禁用DNS", "获取DNS为空,不做任何设置");
return false;
}
//禁用dns
List<string[]> newgateways = new List<string[]>();
for (int j = 0; j < getDNSList.Count; j++)
{
//strDns网卡:114.114.114.114|115.115.115.115|116.116.116.116
string[] strDns = getDNSList[j];
//禁用dns
string[] forbiddenDns = new string[strDns.Length];
for (int i = 0; i < strDns.Length; i++)
{
//dns:1.1.1.1
forbiddenDns[i] = string.Format("{0}.{0}.{0}.{0}", i + 1);
}
//需要设置禁用的dns为1.1.1.1,默认不处理
if (string.Equals(string.Join("|", strDns), string.Join("|", forbiddenDns), StringComparison.OrdinalIgnoreCase))
{
VertifyPwd.isWlan = !setConfig.isForbiddenNetWork;
ErrorCollectHelper.InfoLog("禁用DNS", "DNS为1.1.1.1等默认值,不做任何设置");
return false;
}
else//保存dns状态,可能多网卡,目前最多考虑双网卡
{
if (j == 0)
{
setConfig.dns0 = string.Join("|", strDns);
}
else if (j == 1)
{
setConfig.dns1 = string.Join("|", strDns);
}
}
//禁用dns
newgateways.Add(forbiddenDns);
}
NetWorkSettingHelper.SetDNS(newgateways);
//保存dns
setConfig.isForbiddenNetWork = true;
VertifyPwd.isWlan = false;
setConfig.Save();
}
else//启用dns
{
//默认最多考虑双网卡
List<string[]> getDNSList = new List<string[]>();
if (!string.IsNullOrEmpty(setConfig.dns0))
{
getDNSList.Add(setConfig.dns0.Split('|'));
}
if (!string.IsNullOrEmpty(setConfig.dns1))
{
getDNSList.Add(setConfig.dns1.Split('|'));
}
if (getDNSList.Count > 0)
{
NetWorkSettingHelper.SetDNS(getDNSList);
setConfig.isForbiddenNetWork = false;
VertifyPwd.isWlan = true;
setConfig.Save();
return true;
}
else
{
VertifyPwd.isWlan = !setConfig.isForbiddenNetWork;
ErrorCollectHelper.InfoLog("启用DNS", "没有找到保存的DNS,无法启用");
return false;
}
}
return true;
}
catch (Exception ex)
{
ErrorCollectHelper.ErrorLog("设置网络访问错误", ex.ToString());
return false;
}
}
public bool ForbiddenNetWork(bool isForbidden)
{
try
{
//禁用dns
if (isForbidden)
{
//如果双网卡:114.114.114.114|115.115.115.115|116.116.116.116 8.8.8.8
List<string[]> getDNSList = NetWorkSettingHelper.GetDNS();
if (getDNSList.Count <= 0)
{
VertifyPwd.isWlan = !setConfig.isForbiddenNetWork;
ErrorCollectHelper.InfoLog("禁用DNS", "获取DNS为空,不做任何设置");
return false;
}
//禁用dns
List<string[]> newgateways = new List<string[]>();
for (int j = 0; j < getDNSList.Count; j++)
{
//strDns网卡:114.114.114.114|115.115.115.115|116.116.116.116
string[] strDns = getDNSList[j];
//禁用dns
string[] forbiddenDns = new string[strDns.Length];
for (int i = 0; i < strDns.Length; i++)
{
//dns:1.1.1.1
forbiddenDns[i] = string.Format("{0}.{0}.{0}.{0}", i + 1);
}
//需要设置禁用的dns为1.1.1.1,默认不处理
if (string.Equals(string.Join("|", strDns), string.Join("|", forbiddenDns), StringComparison.OrdinalIgnoreCase))
{
VertifyPwd.isWlan = !setConfig.isForbiddenNetWork;
ErrorCollectHelper.InfoLog("禁用DNS", "DNS为1.1.1.1等默认值,不做任何设置");
return false;
}
else//保存dns状态,可能多网卡,目前最多考虑双网卡
{
if (j == 0)
{
setConfig.dns0 = string.Join("|", strDns);
}
else if (j == 1)
{
setConfig.dns1 = string.Join("|", strDns);
}
}
//禁用dns
newgateways.Add(forbiddenDns);
}
NetWorkSettingHelper.SetDNS(newgateways);
//保存dns
setConfig.isForbiddenNetWork = true;
VertifyPwd.isWlan = false;
setConfig.Save();
}
else//启用dns
{
//默认最多考虑双网卡
List<string[]> getDNSList = new List<string[]>();
if (!string.IsNullOrEmpty(setConfig.dns0))
{
getDNSList.Add(setConfig.dns0.Split('|'));
}
if (!string.IsNullOrEmpty(setConfig.dns1))
{
getDNSList.Add(setConfig.dns1.Split('|'));
}
if (getDNSList.Count > 0)
{
NetWorkSettingHelper.SetDNS(getDNSList);
setConfig.isForbiddenNetWork = false;
VertifyPwd.isWlan = true;
setConfig.Save();
return true;
}
else
{
VertifyPwd.isWlan = !setConfig.isForbiddenNetWork;
ErrorCollectHelper.InfoLog("启用DNS", "没有找到保存的DNS,无法启用");
return false;
}
}
return true;
}
catch (Exception ex)
{
ErrorCollectHelper.ErrorLog("设置网络访问错误", ex.ToString());
return false;
}
}
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;
}
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
View Code
View Code
View Code
互连网设置的一个Helper
网络设置的一个Helper
禁用USB
禁用USB
public class NetWorkSettingHelper
{
public static void SetDNS(List<string[]> getways)
{
try
{
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = wmi.GetInstances();
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
int n = 0;
foreach (ManagementObject mo in moc)
{
//如果没有启用IP设置的网络设备则跳过
if (!(bool)mo["IPEnabled"])
continue;
//设置网关地址
if (getways.Count > 0)
{
//如果dns为空,则if判断永远为false,因此注释
//if (mo["DNSServerSearchOrder"] != null && (mo["DNSServerSearchOrder"] as String[])[0] != null)
//{
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = getways[n++];
outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
// }
}
mo.Clone();
}
}
catch (Exception ex)
{
throw ex;
}
}
public static List<string[]> GetDNS()
{
try
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection nics = mc.GetInstances();
List<string[]> getways = new List<string[]>();
foreach (ManagementObject nic in nics)
{
if (Convert.ToBoolean(nic["ipEnabled"]))
{
if (nic["DNSServerSearchOrder"] != null && (nic["DNSServerSearchOrder"] as String[])[0] != null)
{
getways.Add(nic["DNSServerSearchOrder"] as String[]);
}
nic.Clone();
}
}
return getways;
}
catch (Exception ex)
{
throw ex;
}
}
}
public class NetWorkSettingHelper
{
public static void SetDNS(List<string[]> getways)
{
try
{
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = wmi.GetInstances();
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
int n = 0;
foreach (ManagementObject mo in moc)
{
//如果没有启用IP设置的网络设备则跳过
if (!(bool)mo["IPEnabled"])
continue;
//设置网关地址
if (getways.Count > 0)
{
//如果dns为空,则if判断永远为false,因此注释
//if (mo["DNSServerSearchOrder"] != null && (mo["DNSServerSearchOrder"] as String[])[0] != null)
//{
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = getways[n++];
outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
// }
}
mo.Clone();
}
}
catch (Exception ex)
{
throw ex;
}
}
public static List<string[]> GetDNS()
{
try
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection nics = mc.GetInstances();
List<string[]> getways = new List<string[]>();
foreach (ManagementObject nic in nics)
{
if (Convert.ToBoolean(nic["ipEnabled"]))
{
if (nic["DNSServerSearchOrder"] != null && (nic["DNSServerSearchOrder"] as String[])[0] != null)
{
getways.Add(nic["DNSServerSearchOrder"] as String[]);
}
nic.Clone();
}
}
return getways;
}
catch (Exception ex)
{
throw ex;
}
}
}
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;
}
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
View Code
View Code
View Code
初叶默许dns存在配置文件里,setConfig正是非常存款和储蓄的
初始默认dns存在配置文件里,setConfig便是13分存款和储蓄的
认清得到操作系统位数
判断获得操作系统位数
(上一个是启用禁用USB)
(上2个是启用禁用USB)
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 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
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;
}
}
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
View Code
如上内容都以相比较固化死的,没什么技术含量,那一个内容能够直接拿着作为helper用了
上述内容都以比较固化死的,没什么技术含量,那一个内容能够一贯拿着作为helper用了
(下1个是启用禁止使用外网)
(下一个是启用禁止使用外网)