C#检测是否有u盘插入的方法

本文实例讲述了C#检测是否有u盘插入的方法。分享给大家供大家参考。具体如下:

该C#代码可监控是否有u盘插入,同时可以监控其它驱动器的变化

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices
;
namespace WindowsApplication16
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
  [StructLayout(LayoutKind.Sequential)]
  public struct DEV_BROADCAST_VOLUME
  {
   public int dbcv_size;
   public int dbcv_devicetype;
   public int dbcv_reserved;
   public int dbcv_unitmask;
  }
  protected override void WndProc(ref Message m)
  {
   // 发生设备变动
   const int WM_DEVICECHANGE = 0x0219;
   // 系统检测到一个新设备
   const int DBT_DEVICEARRIVAL = 0x8000;
   // 系统完成移除一个设备
   const int DBT_DEVICEREMOVECOMPLETE = 0x8001;
   // 逻辑卷标
   const int DBT_DEVTYP_VOLUME = 0x00000002;
   switch (m.Msg)
   {
    case WM_DEVICECHANGE:
     switch (m.WParam.ToInt32())
     {
      case DBT_DEVICEARRIVAL:
       int devType = Marshal.ReadInt32(m.LParam, 4);
       if (devType == DBT_DEVTYP_VOLUME)
       {
        DEV_BROADCAST_VOLUME vol;
        vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(
         m.LParam, typeof(DEV_BROADCAST_VOLUME));
        MessageBox.Show(vol.dbcv_unitmask.ToString("x"));
       }
       break;
      case DBT_DEVICEREMOVECOMPLETE:
       MessageBox.Show("Removal");
       break;
     }
     break;
   }
   base.WndProc(ref m);
  }
 }
}

希望本文所述对大家的C#程序设计有所帮助。

(0)

相关推荐

  • C#实现获取磁盘空间大小的方法

    本文实例讲述了C#实现获取磁盘空间大小的方法.分享给大家供大家参考.具体实现方法如下: 方法一:利用System.IO.DriveInfo.GetDrives方法来获取 复制代码 代码如下: ///   /// 获取指定驱动器的空间总大小(单位为B) ///   ///  只需输入代表驱动器的字母即可 (大写) ///    public static long GetHardDiskSpace(string str_HardDiskName) {     long totalSize= new

  • C# 键盘Enter键取代Tab键实现代码

    说明:在填写表数据时当输入完一个文本框后,输入下一个文本框时需要用Tab键切换,但是有的人喜欢用Enter键切换下一个,此方法是Enter取代Tab键. 效果: 注释:键盘Enter键数字为:13 键盘Tab键数字为:9 函数代码: 复制代码 代码如下: private void SetEnt()        {            foreach (Control txt in (this.Page.Form.FindControl("ContentPlaceHolderEntity&qu

  • C#获得MAC地址(网卡序列号)的实现代码

    代码如下: 复制代码 代码如下: //获得网卡序列号    public string GetMoAddress()    {        string MoAddress = " ";        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");        ManagementObjectCollection moc2 = mc.GetInstanc

  • C#获取硬盘编号的方法

    本文实例讲述了C#获取硬盘编号的方法.分享给大家供大家参考.具体实现方法如下: ManagementClass mc = new ManagementClass("Win32_PhysicalMedia"); //Win32_DiskDrive不包含SerialNumber属性. ManagementObjectCollection moc = mc.GetInstances(); string strID = null ; foreach( ManagementObject mo i

  • 用C#获取硬盘序列号,CPU序列号,网卡MAC地址的源码

    privatestring[]GetMoc() { string[]str=newstring[3]; ManagementClassmcCpu=newManagementClass("win32_Processor"); ManagementObjectCollectionmocCpu=mcCpu.GetInstances(); foreach(ManagementObjectminmocCpu) { str[0]=m["ProcessorId"].ToStrin

  • C#中winform实现自动触发鼠标、键盘事件的方法

    程序触发鼠标.键盘事件是C#程序设计中比较常见的功能,本文实例展示了C#中winform实现自动触发鼠标.键盘事件的方法,有不错的实用价值.具体如下: 要想在C#程序中触发鼠标.键盘事件就必须要调用windows函数. 一.鼠标事件的触发 1.引用windows函数mouse_event /// <summary> /// 鼠标事件 /// </summary> /// <param name="flags">事件类型</param> /

  • C#获取U盘序列号的方法

    本文实例讲述了C#获取U盘序列号的方法.分享给大家供大家参考.具体如下: using System.Management; private List<string> _serialNumber = new List<string>(); /// <summary> /// 调用这个函数将本机所有U盘序列号存储到_serialNumber中 /// </summary> private void matchDriveLetterWithSerial() { s

  • c#判断磁盘驱动器类型的两种方法介绍

    1.使用WINDOWS API 复制代码 代码如下: /// <summary> /// 判断一个磁盘驱动器的类型 /// </summary> /// <param name="nDrive">包含了驱动器根目录路径的一个字串</param> /// <returns>Long,如驱动器不能识别,则返回零.如指定的目录不存在,则返回1.如执行成功,则用下述任何一个常数指定驱动器类型:DRIVE_REMOVABLE, DRIV

  • C#利用win32 Api 修改本地系统时间、获取硬盘序列号

    C#利用win32 Api 修改本地系统时间.获取硬盘序列号,可以用于软件注册机制的编写! 复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace Fengyun {     public class Win32     {         #region 修改本地系统时间         [DllIm

  • C#检测pc光驱里是否插入了光盘的方法

    本文实例讲述了C#检测pc光驱里是否插入了光盘的方法.分享给大家供大家参考.具体如下: C# 检测pc光驱里是否插入了光盘,需要添加System.Management.dll 的引用 using System; using System.Management; namespace CDROMManagement { class WMIEvent { static void Main(string[] args) { WMIEvent we = new WMIEvent(); Management

随机推荐