Click here to Skip to main content
Rate this: bad
good
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
Comments
Sergey Alexandrovich Kryukov at 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 at 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
good
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#[^].
 
[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:
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
  Permalink  
v4
Comments
Hetal Jariwala at 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 at 28-Dec-12 1:06am
   
can u help me out??
Sergey Alexandrovich Kryukov at 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 at 2-Jan-13 3:21am
   
Hey can u help me out to find the handler of pendrive
Hetal Jariwala at 2-Jan-13 3:21am
   
Hey can u help me out to find the handler of pendrive
Sergey Alexandrovich Kryukov at 2-Jan-13 10:45am
   
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?
—SA
Hetal Jariwala at 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 ???
Sergey Alexandrovich Kryukov at 16-Jan-13 1:02am
   
I answered to this, please see my updated answer, after [EDIT].
—SA
Abhinav S at 16-Jan-13 1:33am
   
5 for the answer.
Sergey Alexandrovich Kryukov at 16-Jan-13 1:47am
   
Thank you, Abhinav.
—SA

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

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 259
1 Maciej Los 240
2 BillWoodruff 240
3 OriginalGriff 220
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
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