How to detect an USB/SD drive by it´s name in C# (winform)

Andi 111 Reputation points
2022-09-29T07:45:29.353+00:00

Hi,

i want to detect an USB or SD Drive by it´s name. Because it is always the same name but may be on another COM-Port. I need to copy files from it, to it and delete files from it (that i already know) but i dont know how to detect the SD-Card at all..
Is there an easy solution for it?

The actual code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
using System.Runtime.InteropServices;

namespace Evatec_Update_Installer
{
public partial class Step7Screen : Form
{

    public Step7Screen()  
    {  
        InitializeComponent();  

    }  

    private void Step7Screen_Load(object sender, EventArgs e)  
    {  

    }  

    private void FormNext_Click_1(object sender, EventArgs e)  
    {  
        this.Hide();  
        Step8Screen sF = new Step8Screen();  
        sF.Show();  
    }  

    private void BTNStartUpdate_Click(object sender, EventArgs e)  
    {  
        //FormBack.Enabled = false;  
        button1.Enabled = false;  
    }  

    private void button1_Click(object sender, EventArgs e)  
    {  
        //FormBack.Enabled = false;  
        groupBox1.Visible = true;  
        groupBox1.Enabled = true;  
    }  

    private void FormClose_Click(object sender, EventArgs e)  
    {  
        System.Windows.Forms.Application.Exit();  
    }  

    private void Step7Screen_FormClosing(object sender, FormClosingEventArgs e)  
    {  
        System.Windows.Forms.Application.Exit();  
    }  

    private void FormBack_Click(object sender, EventArgs e)  
    {  
        this.Hide();  
        Step6Screen sF = new Step6Screen();  
        sF.Show();  
    }  

    private void button22_Click(object sender, EventArgs e)  
    {  
        pictureBox2.Visible = true;  
        button22.Enabled = false;  
        progressBar2.Visible = true;  

        label8.Visible = true;  
        label8.ForeColor = System.Drawing.Color.Blue;  
        label8.Text = "Please insert Safety SD-Card...";  
        progressBar2.Value = 4;  

        label8.ForeColor = System.Drawing.Color.Orange;  
        label8.Text = "Safety SD-Card detecting...";  
        progressBar2.Value = 8;  
        label8.ForeColor = System.Drawing.Color.Black;  
        label8.Text = "Safety SD-Card detected...";  
        progressBar2.Value = 12;  

        label8.ForeColor = System.Drawing.Color.Orange;  
        label8.Text = "Safety SD-Card backing up...";  
        progressBar2.Value = 16;  
        label8.ForeColor = System.Drawing.Color.Green;  
        label8.Text = "Safety SD-Card backed up...";  
        progressBar2.Value = 20;  

        label8.ForeColor = System.Drawing.Color.Orange;  
        label8.Text = "Safety SD-Card deleting files...";  
        progressBar2.Value = 24;  
        label8.ForeColor = System.Drawing.Color.Green;  
        label8.Text = "Safety SD-Card files deleted...";  
        progressBar2.Value = 28;  

        label8.ForeColor = System.Drawing.Color.Orange;  
        label8.Text = "Safety SD-Card copying new files...";  
        progressBar2.Value = 32;  
        label8.ForeColor = System.Drawing.Color.Green;  
        label8.Text = "Safety SD-Card new files copied...";  
        progressBar2.Value = 36;  

        label8.ForeColor = System.Drawing.Color.Blue;  
        label8.Text = "Please remove Safety SD-Card...";  
        progressBar2.Value = 40;  
        label8.ForeColor = System.Drawing.Color.Black;  
        label8.Text = "Safety SD-Card removed...";  
        progressBar2.Value = 45;  

        label8.ForeColor = System.Drawing.Color.Green;  
        label8.Text = "Safety SD-Card updated";  
        progressBar2.Value = 50;  





        label9.Visible = true;  
        label9.ForeColor = System.Drawing.Color.Blue;  
        label9.Text = "Please insert Standard SD-Card...";  
        progressBar2.Value = 54;  

        label9.ForeColor = System.Drawing.Color.Orange;  
        label9.Text = "Standard SD-Card detecting...";  
        progressBar2.Value = 58;  
        label9.ForeColor = System.Drawing.Color.Black;  
        label9.Text = "Standard SD-Card detected...";  
        progressBar2.Value = 62;  

        label9.ForeColor = System.Drawing.Color.Orange;  
        label9.Text = "Standard SD-Card backing up...";  
        progressBar2.Value = 66;  
        label9.ForeColor = System.Drawing.Color.Green;  
        label9.Text = "Standard SD-Card backed up...";  
        progressBar2.Value = 70;  

        label9.ForeColor = System.Drawing.Color.Orange;  
        label9.Text = "Standard SD-Card deleting files...";  
        progressBar2.Value = 74;  
        label9.ForeColor = System.Drawing.Color.Green;  
        label9.Text = "Standard SD-Card files deleted...";  
        progressBar2.Value = 78;  

        label9.ForeColor = System.Drawing.Color.Orange;  
        label9.Text = "Standard SD-Card copying new files...";  
        progressBar2.Value = 82;  
        label9.ForeColor = System.Drawing.Color.Green;  
        label9.Text = "Standard SD-Card new files copied...";  
        progressBar2.Value = 86;  

        label9.ForeColor = System.Drawing.Color.Blue;  
        label9.Text = "Please remove Standard SD-Card...";  
        progressBar2.Value = 90;  
        label9.ForeColor = System.Drawing.Color.Black;  
        label9.Text = "Standard SD-Card removed...";  
        progressBar2.Value = 95;  

        label9.ForeColor = System.Drawing.Color.Green;  
        label9.Text = "Standard SD-Card updated";  
        progressBar2.Value = 100;  

        pictureBox2.Visible = false;  
        progressBar2.Enabled = false;  
    }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,908 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 86,311 Reputation points
    2022-09-29T08:31:46.75+00:00

    You can use ManagementEventWatcher
    For my USB drives/sticks, I use the query :

            string sQuery =  
                "SELECT * FROM __InstanceCreationEvent " +  
                "WITHIN 2 "  
              + "WHERE TargetInstance ISA 'Win32_PnPEntity'";  
    
            ManagementEventWatcher watcher = new ManagementEventWatcher();  
            WqlEventQuery query = new WqlEventQuery(sQuery);  
    
            watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);  
            watcher.Query = query;  
            watcher.Start();  
    

    then in watcher_EventArrived you can check properties to find your device name :

        static void watcher_EventArrived(object sender, EventArrivedEventArgs e)  
        {  
            ManagementBaseObject mbo = (ManagementBaseObject)e.NewEvent["TargetInstance"];  
            Console.WriteLine(" Guid = " + mbo["ClassGuid"].ToString());  
            foreach (var property in mbo.Properties)  
            {                 
                Console.WriteLine(property.Name + " = " + property.Value);  
            }  
            // other code  
        }  
    

1 additional answer

Sort by: Most helpful
  1. HugeZhong 106 Reputation points
    2022-09-29T08:28:13.93+00:00

    You can use DeviceWatcher from Windows Runtime API, see this link: https://learn.microsoft.com/en-us/uwp/api/windows.devices.enumeration.devicewatcher?view=winrt-19041.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.