使用C#检测Compact Framework中的USB设备插入和删除 [英] Detect USB Device insert and removal in Compact Framework using C#
本文介绍了使用C#检测Compact Framework中的USB设备插入和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;
namespace BadgerMeter.FieldSoftware.RouteReading
{
public class NotifyWindowMessage
{
public event System.EventHandler OnStateChange;
private WindowSink windowSink;
private int uID = 5000;
public bool notifyDev = false;
public NotifyDevice nDev;
public NotifyWindowMessage()
{
windowSink = new WindowSink(this);
windowSink.uID = uID;
windowSink.nDev = notifyDev;
}
~NotifyWindowMessage()
{
// Remove();
}
#region -- API Declarations--
const Int32 DBT_DEVICEARRIVAL = 0X8000;
const Int32 DBT_DEVICEREMOVECOMPLETE = 0X8004;
const Int32 WM_DEVICECHANGE = 0X219;
public static Guid GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");
#endregion
#region class WindowSink
internal class WindowSink : MessageWindow
{
private int m_uID = 0;
public bool nDev;
private NotifyWindowMessage notifyMsg;
public WindowSink(NotifyWindowMessage notiMsg)
{
notifyMsg = notiMsg;
}
public int uID
{
set
{
m_uID = value;
}
}
protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
{
if (msg.Msg == WM_DEVICECHANGE)
{
}
if ((msg.WParam.ToInt32() == DBT_DEVICEARRIVAL))
{
notifyMsg.notifyDev = nDev = true;
if (notifyMsg.OnStateChange != null)
notifyMsg.OnStateChange(notifyMsg, null);
}
else if ((msg.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE))
{
nDev = false;
MessageBox.Show("device removed");
}
}
}
#endregion
}
}
在您的表单或课程中添加以下内容。
On your form or class add following.
public class MermoryStickWatcher : Form
{
public NotifyWindowMessage notifyMsg;
public NotifyDevice notifyDevice;
public MermoryStickWatcher()
{
notifyMsg = new NotifyWindowMessage();
notifyMsg.OnStateChange += new EventHandler(OnStateChanged);
}
public void OnStateChanged(object sender, EventArgs e)
{
bool Attempt;
if (notifyMsg.notifyDev)
{
DirectoryInfo dirInfo = new DirectoryInfo("/Hard Disk/Load/");
//Check if Directory structure are there
if (dirInfo.Exists)
{
this.ValidateDirectory();
}
else
{
//If Directory Structure are not there Foreign memory stick,then dont initialize, display a message for that with yes no
Attempt = TextDialog.Show("Unrecognized memory stick! Do you want to Associate with Handheld? ",
"Unrecognized memory stick! ",
TextDialogButtons.YesNo) == TextDialogResult.Yes;
if (Attempt)
{
//Create File Structure
DirectoryInfo di = Directory.CreateDirectory("/Hard Disk/Load/");
this.ValidateDirectory();
}
}
}
}
}
推荐答案
由于问题不完整所以请提供有关您问题的更多详细信息
但我想这些文章可以帮助您 -
在C#程序中检测USB驱动器的移除或插入 [ ^ ]
检测硬件插入和/或删除 [< a href =http://www.codeproject.com/Articles/14500/Detecting-Hardware-Insertion-and-or-Removaltarget =_ blanktitle =New Window> ^ ]
USB库到检测USB设备 [ ^ ]
在Windows服务中接收设备事件通知(检测服务中的硬件插入和/或删除) [ ^ ]
As the question is incomplete so provide more details about your question
But I guess these article can help you-
Detecting USB Drive Removal or Insertion in a C# Program[^]
Detecting Hardware Insertion and/or Removal[^]
A USB Library to Detect USB Devices[^]
Receiving Device Event Notification in Windows Service(Detecting Hardware Insertion and/or Removal in Service)[^]
这篇关于使用C#检测Compact Framework中的USB设备插入和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文