Click here to Skip to main content
11,931,584 members (54,410 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Windows .NET
I need to know a method by which i can know Pen drice instance handler id.
camn any one suggest??
Posted 27-Dec-12 3:04am
Sergey Alexandrovich Kryukov 27-Dec-12 15:23pm
Excuse me, what do you mean by "drive instance handler"? In other words, what do you want to do with this handler? Most basic chores could be done by System.Management (WMI)...
Hetal Jariwala 27-Dec-12 23:23pm
I want programatiically remove pen drive from system
for that i need to know the instance handler

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

As I say, this is easy to do via System.Management.

Naturally, there is no such such concept as "thumb drive". There is a concept of removable drive. This is a drive with DriveType 2. You need to scan all available volumes and find out a removable drive, or a set of drives. Then you can unmount one or more of them. When this is done, OS guarantees that you can safely remove the drive/card, because all previously uncommitted is not committed.

The management object you need to obtain is Win32_Volume. All volumes are obtained via the WMI query "SELECT * FROM Win32_Volume WHERE ...".

Please also see this CodeProject article: Eject USB disks using C#[^].


Answering follow-up questions:

You don't have access to Win32_Volume directly as it would be a .NET class. WMI gives you reflective interface to them. (Not to mix up with .NET reflection.) You can get properties and methods my names and invoke them (well, unfortunately, this is how it's done).

For example, here is how to dismount removable drives:
using System;
using System.Management;

            ManagementObjectSearcher searcher =
               new ManagementObjectSearcher(
                   "SELECT * FROM Win32_Volume WHERE DriveType=2");
            ManagementObjectCollection collection = searcher.Get();
            foreach (ManagementObject item in collection) {
                //show drive name:
                System.Console.WriteLine("Name: {0}", item["Name"]);
                var result =
                       new object[] { false, false });
                if (result != 0) {/* show error */}
            } // loop

You can get documentation for Win32_Volume separately, from C++ API and call methods/properties by their documented names.

Hetal Jariwala 28-Dec-12 1:06am
But in the above example
auther is using Cm_Request_Deviec_Eject method
i cann't find out, how user is getting handler for local computer
Hetal Jariwala 28-Dec-12 1:06am
can u help me out??
I'll try to, but it's too late in my time zone, I will need some more time, and will be probably too busy tomorrow, but will try to find some time. New Year is coming, still too much to do. :-)
Last time I did it using PowerShell; it's also .NET, but with a twist. It looks like:

$drives = Get-WmiObject Win32_volume | where { $_.DriveType -eq 2 }

And it returns the collection of Win32_volume objects, wrapped. It should give you the idea how to do the query using C# and System.Management. Please look at the relevant help pages for this name space, you will easily find everything.

You can also ask the author of this article, but this approach is more fundamental and a bit complex, maybe...

If your problem is not yet solved in a while, please remind me by commenting (Reply on this comment).

Good luck,
Hetal Jariwala 2-Jan-13 3:21am
Hey can u help me out to find the handler of pendrive
Hetal Jariwala 2-Jan-13 3:21am
Hey can u help me out to find the handler of pendrive
Do you have a problem to invoke WMI, or something else, such as the query? As far as I understand, all you need is to locate a proper instance of Win32_Volume, right?
Hetal Jariwala 16-Jan-13 0:01am
Ya but how to locate win32_volume handler??
I have gone through the example but i could not recognize how they are doing ???
I answered to this, please see my updated answer, after [EDIT].
Abhinav S 16-Jan-13 1:33am
5 for the answer.
Thank you, Abhinav.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100