admin管理员组文章数量:1567138
using System.Windows.Forms;
using System.IO;
namespace U盘杀毒
{
public partial class Form1 : Form
{
public const int WM_DEVICECHANGE = 0x219;//U盘插入后,OS的底层会自动检测到,然后向应用程序发送“硬件设备状态改变“的消息
public const int DBT_DEVICEARRIVAL = 0x8000; //就是用来表示U盘可用的。一个设备或媒体已被插入一块,现在可用。
public const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //一个设备或媒体片已被删除。
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_DEVICECHANGE){
switch (m.WParam.ToInt32()){
case DBT_DEVICEARRIVAL://U盘插入
DriveInfo[] uin = DriveInfo.GetDrives();
foreach (DriveInfo drive in uin){
if (drive.DriveType == DriveType.Removable){
this.Text = "U盘已插入,盘符为:" + drive.Name.ToString();
break;
}
}
break;
case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
this.Text = "U盘被拔出!";
break;
default:
break;
}
}
base.WndProc(ref m);
}
}
}
转自网络,本人仅作精简
本文标签:
版权声明:本文标题:【C#】监视U盘的插入与拔出 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1727576063a1121624.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论