none
Extended Disk/usb divices

    Question

  • Hello I found a code to internet about how to list all removable devices(USB, external disks etc.)

    but it doesn't work, can someone tell me something else or to add some code:

    using System.IO;

    var driveList = DriveInfo.GetDrives(); foreach (DriveInfo drive in driveList) { if (drive.DriveType == DriveType.Removable) { ListViewItem item = new ListViewItem(drive.Name); item.SubItems.Add(drive.RootDirectory.ToString()); item.SubItems.Add(drive.TotalSize.ToString()); item.SubItems.Add(drive.TotalFreeSpace.ToString()); } }

    thanks

    Wednesday, April 12, 2017 11:36 AM

Answers

  • I tested with a USB Stick and it works for me (Windows 10)

    Otherwise, you can also use WMI (Win32_DiskDrive)

    Wednesday, April 12, 2017 12:20 PM
  • 1. Make sure you have removable drives connected on the system you are running this code.

    Since this code is adding items in a listViewItem only. You must have a list view in your application and the ListViewItem (per drive found is binded to ListView) .

    Working with ListView in C#

    For checking in console you can check during debug by adding below line

       Console.Write(drive.RootDirectory.ToString() + " - " + drive.TotalSize.ToString() + " - " + drive.TotalFreeSpace.ToString());

     


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, April 12, 2017 12:20 PM

All replies

  • I tested with a USB Stick and it works for me (Windows 10)

    Otherwise, you can also use WMI (Win32_DiskDrive)

    Wednesday, April 12, 2017 12:20 PM
  • 1. Make sure you have removable drives connected on the system you are running this code.

    Since this code is adding items in a listViewItem only. You must have a list view in your application and the ListViewItem (per drive found is binded to ListView) .

    Working with ListView in C#

    For checking in console you can check during debug by adding below line

       Console.Write(drive.RootDirectory.ToString() + " - " + drive.TotalSize.ToString() + " - " + drive.TotalFreeSpace.ToString());

     


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, April 12, 2017 12:20 PM