Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# USB Detection [modified]

C# USB Detection [modified]

Scheduled Pinned Locked Moved C#
csharpquestionwpf
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    donovan solms
    wrote on last edited by
    #1

    hi, I've tried the following code at http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic6944.aspx i've made the changes they say, but it still doesn't work when i insert a usb device (flashdisk). I'm trying do detect when a device is inserted into a usb port. how can i do it? and if possible, which port it was inserted at. it's for a WPF app EDIT: ok, it's working now, but i have the same questions that the person with the code posted: 1)Check if the device is inserted or removed..How do i do that?? 2)also i want to check Total storage (capacity)of the device connected c) storage(unused)available in the device thanks donovan -- modified at 6:54 Thursday 12th July, 2007

    rather have something you don't need, than need something you don't have

    G M 2 Replies Last reply
    0
    • D donovan solms

      hi, I've tried the following code at http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic6944.aspx i've made the changes they say, but it still doesn't work when i insert a usb device (flashdisk). I'm trying do detect when a device is inserted into a usb port. how can i do it? and if possible, which port it was inserted at. it's for a WPF app EDIT: ok, it's working now, but i have the same questions that the person with the code posted: 1)Check if the device is inserted or removed..How do i do that?? 2)also i want to check Total storage (capacity)of the device connected c) storage(unused)available in the device thanks donovan -- modified at 6:54 Thursday 12th July, 2007

      rather have something you don't need, than need something you don't have

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      Maybe this can help you http://www.codeproject.com/cs/system/DriveDetector.asp ? As for storage Use GetDrives() function to get all drives and identify the drive you are interested in. Then you can use DriveInfo functions to get necessary information

      #region signature my articles #endregion

      1 Reply Last reply
      0
      • D donovan solms

        hi, I've tried the following code at http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic6944.aspx i've made the changes they say, but it still doesn't work when i insert a usb device (flashdisk). I'm trying do detect when a device is inserted into a usb port. how can i do it? and if possible, which port it was inserted at. it's for a WPF app EDIT: ok, it's working now, but i have the same questions that the person with the code posted: 1)Check if the device is inserted or removed..How do i do that?? 2)also i want to check Total storage (capacity)of the device connected c) storage(unused)available in the device thanks donovan -- modified at 6:54 Thursday 12th July, 2007

        rather have something you don't need, than need something you don't have

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        Hello, I'm using System.Management functionality for that!

        //Insert
        WqlEventQuery q_creation = new WqlEventQuery();
        q_creation.EventClassName = "__InstanceCreationEvent";
        q_creation.WithinInterval = new TimeSpan(0,0,2); //How often do you want to check it? 2Sec.
        q_creation.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'";
        mwe_creation = new ManagementEventWatcher(q_creation);
        mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);
        mwe_creation.Start(); // Start listen for events

        internal void USBEventArrived_Creation(object sender, EventArrivedEventArgs e)
        {

        }

        //Remove
        WqlEventQuery q_deletion = new WqlEventQuery();
        q_deletion.EventClassName = "__InstanceDeletionEvent";
        q_deletion.WithinInterval = new TimeSpan(0,0,2); //How often do you want to check it? 2Sec.
        q_deletion.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition' ";
        mwe_deletion = new ManagementEventWatcher(q_deletion);
        mwe_deletion.EventArrived += new EventArrivedEventHandler(USBEventArrived_Deletion);
        mwe_deletion.Start(); // Start listen for events

        internal void USBEventArrived_Deletion(object sender, EventArrivedEventArgs e)
        {

        }

        For checking the free and available space:

        public void Hashtable GetFreeDriveSpace()
        {
        Hashtable drivefreespace = new Hashtable();
        // Get the management class holding Logical Drive information
        ManagementClass mcDriveClass = new ManagementClass("Win32_LogicalDisk");
        // Enumerate all logical drives available
        ManagementObjectCollection mocDrives = mcDriveClass.GetInstances();
        foreach(ManagementObject moDrive in mocDrives)
        {
        /*
        Other values of DriveType property:
        0 Unknown
        1 No Root Directory
        2 Removable Disk
        3 Local Disk
        4 Network Drive
        5 Compact Disc
        6 RAM Disk
        */
        try
        {
        //if (int.Parse(moDrive.Properties["DriveType"].Value.ToString()) == 3) //You can check the drive type here
        //{
        // sDeviceId will hold the drive name eg "C:"
        String sDeviceId = moDrive.Properties["DeviceId"].Value.ToString();
        // dSize and dFree will hold the size of the drive and free space in bytes
        double dSize = double.Parse(moDrive.Properties["Size"].Value.ToString());
        double dFree = double.Parse(moDrive.Properties["FreeSpace"].Value.ToString()); //In Byte
        //}
        }
        catch
        {
        }
        }
        mocDrives.Dispose();
        mcDriveClass.Dispose();
        }

        Hope it helps!

        All the best, Martin

        D 1 Reply Last reply
        0
        • M Martin 0

          Hello, I'm using System.Management functionality for that!

          //Insert
          WqlEventQuery q_creation = new WqlEventQuery();
          q_creation.EventClassName = "__InstanceCreationEvent";
          q_creation.WithinInterval = new TimeSpan(0,0,2); //How often do you want to check it? 2Sec.
          q_creation.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'";
          mwe_creation = new ManagementEventWatcher(q_creation);
          mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);
          mwe_creation.Start(); // Start listen for events

          internal void USBEventArrived_Creation(object sender, EventArrivedEventArgs e)
          {

          }

          //Remove
          WqlEventQuery q_deletion = new WqlEventQuery();
          q_deletion.EventClassName = "__InstanceDeletionEvent";
          q_deletion.WithinInterval = new TimeSpan(0,0,2); //How often do you want to check it? 2Sec.
          q_deletion.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition' ";
          mwe_deletion = new ManagementEventWatcher(q_deletion);
          mwe_deletion.EventArrived += new EventArrivedEventHandler(USBEventArrived_Deletion);
          mwe_deletion.Start(); // Start listen for events

          internal void USBEventArrived_Deletion(object sender, EventArrivedEventArgs e)
          {

          }

          For checking the free and available space:

          public void Hashtable GetFreeDriveSpace()
          {
          Hashtable drivefreespace = new Hashtable();
          // Get the management class holding Logical Drive information
          ManagementClass mcDriveClass = new ManagementClass("Win32_LogicalDisk");
          // Enumerate all logical drives available
          ManagementObjectCollection mocDrives = mcDriveClass.GetInstances();
          foreach(ManagementObject moDrive in mocDrives)
          {
          /*
          Other values of DriveType property:
          0 Unknown
          1 No Root Directory
          2 Removable Disk
          3 Local Disk
          4 Network Drive
          5 Compact Disc
          6 RAM Disk
          */
          try
          {
          //if (int.Parse(moDrive.Properties["DriveType"].Value.ToString()) == 3) //You can check the drive type here
          //{
          // sDeviceId will hold the drive name eg "C:"
          String sDeviceId = moDrive.Properties["DeviceId"].Value.ToString();
          // dSize and dFree will hold the size of the drive and free space in bytes
          double dSize = double.Parse(moDrive.Properties["Size"].Value.ToString());
          double dFree = double.Parse(moDrive.Properties["FreeSpace"].Value.ToString()); //In Byte
          //}
          }
          catch
          {
          }
          }
          mocDrives.Dispose();
          mcDriveClass.Dispose();
          }

          Hope it helps!

          All the best, Martin

          D Offline
          D Offline
          donovan solms
          wrote on last edited by
          #4

          thanks to both of you, seems both solutions would be able to solve my problem! thanks again donovan

          rather have something you don't need, than need something you don't have

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups