C# 如何实现u盘检测,有程序正常运行,没有程序进行提示

沧海1梦 沧海1梦 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如题,想实现类似加密狗的功能,如果程序检测到有U盘插入,整个程序正常运行,如果没有,则程序提示请插入U盘,
C#加wpf,以下是我写的,但是msg传递有问题,请大家帮忙看看
  private void Win_Load(object sender, RoutedEventArgs e)
{
System.Windows.Interop.HwndSource hWndSource;
System.Windows.Interop.WindowInteropHelper wih = new System.Windows.Interop.WindowInteropHelper(this);
hWndSource = System.Windows.Interop.HwndSource.FromHwnd(wih.Handle);
//添加处理程序
hWndSource.AddHook(winmsg);
}

public const int WM_DEVICECHANGE = 0x219;
public const int DBT_DEVICEARRIVAL = 0x8000; //如果m.Msg的值为0x8000那么表示有U盘插入
public const int DBT_CONFIGCHANGECANCELED = 0x0019;
public const int DBT_CONFIGCHANGED = 0x0018;
public const int DBT_CUSTOMEVENT = 0x8006;
public const int DBT_DEVICEQUERYREMOVE = 0x8001;
public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
public const int DBT_DEVICEREMOVECOMPLETE = 0X8004;
public const int DBT_DEVICEREMOVEPENDING = 0x8003;
public const int DBT_DEVICETYPESPECIFIC = 0x8005;
public const int DBT_DEVNODES_CHANGED = 0x0007;
public const int DBT_QUERYCHANGECONFIG = 0x0017;
public const int DBT_USERDEFINED = 0xFFFF;
private IntPtr winmsg(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{

try
{
switch (msg)
{
case WM_DEVICECHANGE:
break;
case DBT_DEVICEARRIVAL://U盘插入
string[] strDrivers = Environment.GetLogicalDrives();
break;
case DBT_CONFIGCHANGECANCELED:
break;
case DBT_CONFIGCHANGED:
break;
case DBT_CUSTOMEVENT:
break;
case DBT_DEVICEQUERYREMOVE:
break;
case DBT_DEVICEQUERYREMOVEFAILED:
break;
case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
break;
case DBT_DEVICEREMOVEPENDING:
break;
case DBT_DEVNODES_CHANGED:
break;
case DBT_QUERYCHANGECONFIG:
break;
case DBT_USERDEFINED:
break;
default:
break;
}
}
catch (System.Exception ex)
{
log.Error("U盘检测:" + ex);
}
return IntPtr.Zero;
}
...全文

相关推荐

C#自动检测U盘插入 静默复制U盘文件到本地

C#编写的winform,启动后,自动检测U盘插入,如果有U盘插入电脑,则自动开始后台静默复制U盘文件到本地。

王小波门下走狗

C#实现U盘插拔检测技术指南

即插即用(PnP)技术是一种硬件设备管理机制,允许计算机系统自动识别和配置新硬件,无需用户手动进行复杂配置。在Windows操作系统中,PnP机制扮演着至关重要的角色,它确保了系统能够高效、准确地管理各种外设,特别是U盘这类即插即用设备。Windows Management Instrumentation (WMI) 是Windows操作系统中用于监控和控制系统资源的核心技术。它允许管理员从脚本或命令行获取系统信息,执行管理任务,以及配置和监视应用程序、设备和服务。

西域情歌

开机启动检测U盘程序 并打开主程序

可以开机启动 并检测U盘是否插入 如果插入的话就启动读取U盘的程序

Vitalistic

c#实现u盘操作程序,可进行对u盘的感应及安全卸载

c#实现u盘操作程序,可进行对u盘的感应及安全卸载,插上u盘可对网络资源的自动下载.

lidong_king

C# 获取U盘ID序列号及U盘信息

获取U盘ID序列号 VS2005编译通过,源码源自CSDN。已经测试好用。 可以获得U盘名称,制造商ID号 版本号 U盘序列号及容量 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Management; namespace UDiskTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { DriveInfo[] s = DriveInfo.GetDrives(); foreach (DriveInfo drive in s) { if (drive.DriveType == DriveType.Removable) { pf.Text = drive.Name.ToString(); break; } } ManagementClass cimobject = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc = cimobject.GetInstances(); foreach (ManagementObject mo in moc) { if (mo.Properties["InterfaceType"].Value.ToString() == "USB") { try { //产品名称 Caption.Text = mo.Properties["Caption"].Value.ToString(); //总容量 Size.Text = mo.Properties["Size"].Value.ToString(); string[] info = mo.Properties["PNPDeviceID"].Value.ToString().Split('&'); string[] xx = info[3].Split('\\'); //序列号 MessageBox.Show("U盘序列号:" + xx[1]); PNPDeviceID.Text = xx[1]; xx = xx[0].Split('_'); //版本号 REV.Text = xx[1]; //制造商ID xx = info[1].Split('_'); VID.Text = xx[1]; } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } } } 如果编译遇到问题是using System.Management;无法找到引用找到, 项目(p)->添加引用(R)->找到.NET标签页 列表中下部位找到System.Management 双击点确定

大伟LAW
评论
收藏 已收藏
分享
返回顶部
沧海1梦
C# 111,130 成员 642,541 内容