Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to know a method by which i can know Pen drice instance handler id.
camn any one suggest??
Posted
Comments
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)...
—SA
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

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#[^].

[EDIT]

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:
C#
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 =
                   item.InvokeMethod("Dismount",
                       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.

—SA
 
Share this answer
 
v4
Comments
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??
Sergey Alexandrovich Kryukov 28-Dec-12 1:39am    
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,
—SA
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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900