找传奇、传世资源到传世资源站!

C# USB u盘禁止插入考贝 源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

USB u盘禁止插入考贝,适合,公司,个人
C# USB u盘禁止插入考贝 源码 C#语言基础-第1张
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Microsoft.Win32;using System.Threading;using System.Management;using System.Management.Instrumentation;using System.Security.Cryptography;using System.Timers;using System.Diagnostics;using System.Runtime.InteropServices;using System.IO; namespace USB禁止拷片{ public partial class Form1 : Form { private string p; private string md5machineRegReade = " "; public System.Threading.Timer timer;//模块级变量 private string timer_S = "0000-00-00"; [DllImport("QomoHookSrv.dll", CallingConvention = CallingConvention.Cdecl)] private extern static bool InstallHook(int processID); [DllImport("QomoHookSrv.dll", CallingConvention = CallingConvention.Cdecl)] private extern static bool UninstallHook(); private int CC = 0;//计时启动 private int savecc=0;//保护用时 public Form1() { InitializeComponent(); } public Form1(string p) { InitializeComponent(); this.p = p; } private void Form1_Load(object sender, EventArgs e) { md5regREADE(); GetInfo();//检索机器码 GlobleVar.MD5Machine = GetMD5(GlobleVar.Machine) timer_S;//把获取的字符串转换为日期; clockTF(); Testimer(); } /// <summary> /// 第一个时间线程 /// </summary> public void Testimer() { timer = new System.Threading.Timer(new TimerCallback(timer_Elapsed)); timer.Change(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2)); } /// <summary> /// 第二个时间线程 /// /// </summary> /// <summary> /// 通过注册表启用USB /// </summary> /// <returns></returns> public bool RegToRunUSB() { try { System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { RegistryKey regKey = Registry.LocalMachine; //读取注册列表HKEY_LOCAL_MACHINE string keyPath = @"SYSTEM\CurrentControlSet\Services\USBSTOR"; //USB 大容量存储驱动程序 RegistryKey openKey = regKey.OpenSubKey(keyPath, true); openKey.SetValue("Start", 3); //设置键值对(3)为开启USB(4)为关闭 openKey.Close(); //关闭注册列表读写流 } return true; } catch (Exception ex) { throw ex; } } /// <summary> /// 通过注册表禁用USB /// </summary> /// <returns></returns> public bool RegToStopUSB() { try { System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { RegistryKey regKey = Registry.LocalMachine; string keyPath = @"SYSTEM\CurrentControlSet\Services\USBSTOR"; RegistryKey openKey = regKey.OpenSubKey(keyPath, true); openKey.SetValue("Start", 4); openKey.Close(); } return true; } catch (Exception ex) { throw ex; } } /// <summary> /// 关闭任务管理器 /// </summary> /// <param name="arg"></param> /// /// <summary> /// 管理任务管理器的方法 /// </summary> /// <param name="arg">0:启用任务管理器 1:禁用任务管理器</param> // private void ManageTaskManager(int arg) // { // RegistryKey currentUser = Registry.CurrentUser; // RegistryKey system = currentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", true); //如果system项不存在就创建这个项 // if (system == null) // { // system = currentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System"); // } // system.SetValue("DisableTaskmgr", arg, RegistryValueKind.DWord); // currentUser.Close(); // } private void button1_Click(object sender, EventArgs e) { Form f = new Form2(); f.ShowDialog(); if (GlobleVar.OPEN_yanzheng) { CC = 0;//打开计时GlobleVar.BTN_stack = true; clockTF(); } } private void button2_Click(object sender, EventArgs e) { GlobleVar.BTN_stack = false; clockTF(); if (!md5machineRegReade.Equals(GlobleVar.MD5Machine))//判断是否注册 { MessageBox.Show("未注册.........!请联系系统管理员!", "注册提示窗口"); } } private void button3_Click(object sender, EventArgs e) { GlobleVar.EXit_button = true; Form f = new Form2(); f.ShowDialog(); Application.Exit(); } private void frmFileDisposal_Closed(object sender, System.EventArgs e) { System.Environment.Exit(System.Environment.ExitCode); this.Dispose(); this.Close(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true;//禁止点X退出 if (GlobleVar.OPEN_yanzheng) { if (GlobleVar.wind_close) { uninstall_hook_when_app_closed(); e.Cancel = false; } } } private void timer_Elapsed(object state) { //进入计时状态 if (CC < 20) CC ; if (CC == 20 && GlobleVar.BTN_stack) { GlobleVar.BTN_stack = false;//如果计时到达自动进入U盘禁用状态 } clockTF(); } private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e) { Form f = new Form3(); f.Show(); } private void GetInfo() { //获取硬盘ID try { ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); foreach (ManagementObject mo in searcher.Get()) { GlobleVar.Machine = mo["SerialNumber"].ToString().Trim(); break; } } catch { GlobleVar.Machine = "NOJHUP983256886"; } //获取网卡硬件地址 try { string mac = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) if ((bool)mo["IPEnabled"] == true) { mac = mo["MacAddress"].ToString() " "; break; } moc = null; mc = null; GlobleVar.Machine = mac.Trim().Replace(":", ""); } catch { } } private void 系统注册ToolStripMenuItem_Click(object sender, EventArgs e) { Form f = new Form4(); f.Show(); } public static string GetMD5(string myString) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString); byte[] targetData = md5.ComputeHash(fromData); string byte2String = null; for (int i = 0; i < targetData.Length; i = i 2) { byte2String = targetData[i].ToString("x"); } return byte2String; } public void md5regREADE() { try //可能有异常,放在try块中 { RegistryKey rsg = null; //声明变量 rsg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft", true); //true表可修改 if (rsg.GetValue("MaChineReg") != null) //如果值不为空 { md5machineRegReade = rsg.GetValue("MaChineReg").ToString(); //读取值 //获取字符串(日期) //开始位置 timer_S = md5machineRegReade.Substring(md5machineRegReade.Length - 20, 20); //取搜索的条数,用结束的位置-开始的位置,并返回 //把获取的字符串转换为日期 GlobleVar.time2 = Convert.ToDateTime(Decode(timer_S)); //Convert.ToDateTime(string) //string格式有要求,必须是yyyy-MM-dd hh:mm:ss } else md5machineRegReade = "该键不存在!"; rsg.Close(); //关闭 } catch (Exception ex) //捕获异常 { } } public void clockTF() { if (GlobleVar.BTN_stack) { //如果禁用U盘控制器状态为 true 禁用U盘 RegToRunUSB(); uninstall_hook_when_app_closed(); } else { if (md5machineRegReade.Equals(GlobleVar.MD5Machine))//判断是否注册 { GlobleVar.zhuce = true; if (DateTime.Compare(GlobleVar.now, GlobleVar.time2) > 0) { GlobleVar.jihuo = false; RegToRunUSB(); uninstall_hook_when_app_closed(); //ManageTaskManager(0); } else { GlobleVar.jihuo = true; RegToStopUSB(); install_hook_when_app_startup(); //ManageTaskManager(1); } } else { GlobleVar.zhuce = false; //未注册 GetInfo();//获取机器码 GlobleVar.MD5Machine = GetMD5(GlobleVar.Machine) timer_S;//把获取的字符串转换为日期; md5regREADE(); RegToRunUSB(); uninstall_hook_when_app_closed(); } } } private void menuStrip1_Enter(object sender, EventArgs e) { System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(timer_Elapsed), null, 0, 5000); } //窗口到托盘 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { this.Visible = false; this.Show(); WindowState = FormWindowState.Normal; this.Focus(); } private void Form1_Resize(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) //最小化到系统托盘 { this.Visible = true; //显示托盘图标 this.Hide(); //隐藏窗口 } } /// <summary> /// <函数:Decode> ///作用:将16进制数据编码转化为字符串源码天空,是Encode的逆过程 /// </summary> /// <param name="strDecode"></param> /// <returns></returns> public static string Decode(string strDecode) { string sResult = ""; for (int i = 0; i < strDecode.Length / 2; i ) { sResult = (char)short.Parse(strDecode.Substring(i * 2, 2), global::System.Globalization.NumberStyles.HexNumber); } return sResult; } //钩子进程 private void install_hook_when_app_startup() { //获取进程的ID int pid = System.Diagnostics.Process.GetCurrentProcess().Id; //安装钩子 InstallHook(pid); } private void uninstall_hook_when_app_closed() { UninstallHook(); } private void Form1_Activated(object sender, EventArgs e) { Message_up(); } //U盘显示状态刷新 private void Message_up() { if (GlobleVar.BTN_stack) { textBox1.Text = "状态:U盘开放!"; label1.Text = "警告:U盘限制以经开放……电脑文件未受保护……!"; //以独占方式解除打开文件 GlobleVar.fs.Close(); GlobleVar.fs1.Close(); } else { if (GlobleVar.zhuce) {if (GlobleVar.jihuo){ textBox1.Text = "状态:U盘已禁用"; label1.Text = "恭喜:电脑文件正在受保护!系统已运行:" savecc.ToString() "分钟"; //以独占方式打开文件 try { GlobleVar.fs = new FileStream("C:\\Windows\\inf\\usbstor.inf", FileMode.Open, FileAccess.Read, FileShare.None); GlobleVar.fs1 = new FileStream("C:\\Windows\\inf\\usbstor.PNF", FileMode.Open, FileAccess.Read, FileShare.None); } catch { }}else{ textBox1.Text = "状态:U盘开放!"; label1.Text = "警告:系统验证机制已激活,请联系管理员重新激活系统……!"; //以独占方式解除打开文件 GlobleVar.fs.Close(); GlobleVar.fs1.Close();} } else {textBox1.Text = "状态:U盘开放!";label1.Text = "警告:未注册……!电脑文件无法受到保护……!";//以独占方式解除打开文件 GlobleVar.fs.Close();GlobleVar.fs1.Close(); } } } private void button1_MouseUp(object sender, MouseEventArgs e) { Message_up(); } private void button2_MouseUp(object sender, MouseEventArgs e) { Message_up(); } private void Form1_MouseMove(object sender, MouseEventArgs e) { Message_up(); } private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = false; Message_up(); savecc ; timer1.Enabled = true; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复