实例介绍
【实例截图】
【核心代码】
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;
}
}
}
标签: USB u盘禁止插入考贝
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论