简易U盘自动运行实时保护工具附源程序(转)简易U盘自动运行实时保护工具附源程序(转)
先告诉你,C#写这个实在很XX,没意义,只是论坛有人问,稍加修改而已。这种系统级的东西还是
给C++或者其他语言来比较好。
是由之前写的RDM改来的,很快。
注意IO部分和最后一个chkAutorun()方法函数就知道多easy了- -
当然检测到就delete了=。=
using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;
namespace At....
简易U盘自动运行实时保护工具附源程序(转)
先告诉你,C#写这个实在很XX,没意义,只是论坛有人问,稍加修改而已。这种系统级的东西还是
给C++或者其他语言来比较好。
是由之前写的RDM改来的,很快。
注意IO部分和最后一个chkAutorun()方法函数就知道多easy了- -
当然检测到就delete了=。=
using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;
namespace At.NET
{
public partial class RemovableDevicesMonitor : Form
{
string[] CurrDevices; //发生插入/拔出前有的设备
string[] CurrDevices_; //发生插入/拔出后有的设备
public RemovableDevicesMonitor()
{
InitializeComponent();
//获取插入/拔出前的驱动器列
CurrDevices = Environment.GetLogicalDrives();
}
protected override void WndProc(ref Message m)
{
const int WM_DEVICECHANGE = 0x219; //设备有变动(插入或拔出)
const int WM_DEVICEARRVIAL = 0x8000; //设备有变动(插入)
const int WM_DEVICEMOVECOMPLETE = 0x8004; //设备有变动 (拔出)
switch (m.Msg)
{
case WM_DEVICECHANGE:
{
if (m.WParam.ToInt32() == WM_DEVICEARRVIAL)
{
this.listView1.Items.Add("有设备插入,于"
+ DateTime.Now.ToString() +", 盘符" + this.doCheck (true));
this.CurrDevices = Environment.GetLogicalDrives();//储存现有盘符
this.CurrDevices_ = null;
}
if (m.WParam.ToInt32() == WM_DEVICEMOVECOMPLETE)
{
this.listView1.Items.Add("有设备拔出,于"
+ DateTime.Now.ToString()+", 盘符" + this.doCheck(false));
this.CurrDevices = Environment.GetLogicalDrives();//储存现有盘符
this.CurrDevices_ = null;
}
break;
}
}
base.WndProc(ref m);
}
///
/// 检查插入/拔出设备的盘符
/// true为插入、false为拔出,没有default情况滴~
///
///
///
string 插入\拔出设备名
protected string doCheck(bool isArrival)
{
switch (isArrival)
{
case true: //插入段
{
CurrDevices_ = Environment.GetLogicalDrives(); //获取插入/拔出后的驱动器列表
var al = new ArrayList();
/*
* 这里要注意,插入时驱动器多一个,所以
* 我通过遍历现有驱动器列表检查原有驱动器列表
* 中是否含有某个驱动器,如果不存在则这就是
* 被插入的驱动器。但是下面拔出段的情况是相反的。
*/
//将原有的驱动器列表缓存到ArrayList
foreach (string DeviceName in CurrDevices)
{
al.Add(DeviceName);
}
//遍历现有驱动器列表与原有驱动器列表对比
foreach (string DeviceName_ in CurrDevices_)
{
if (!al.Contains(DeviceName_))
{
chkAutorun(DeviceName_);
return DeviceName_;
//检测到则return,因为只有插入了才有消息,所以基本是100%会return。
}
}
break;
}
case false: //拔出段,思路同插入段。
{
CurrDevices_ = Environment.GetLogicalDrives();
var al_ = new ArrayList();
foreach (string DeviceName_ in CurrDevices_)
{
al_.Add(DeviceName_);
}
foreach (string DeviceName in CurrDevices)
{
if (!al_.Contains(DeviceName))
{
return DeviceName;
}
}
break;
}
default:
{
break;
}
}
return "无法检测";
}
protected void chkAutorun(string DeviceName_)
{
string path = DeviceName_ + "autorun.inf";
if (File.Exists(path))
{
MessageBox.Show("存在自动运行文件!");
//删除自动运行文件
try
{
File.Delete(path);
}
catch { }
}
}
}
}
本文档为【简易U盘自动运行实时保护工具附源程序(转)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。