在c#中获取Pendrive实例处理程序ID [英] Get Pendrive Instance Handler ID in c#

查看:57
本文介绍了在c#中获取Pendrive实例处理程序ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道一种方法,通过它我可以知道Pen drice实例处理程序ID。

camn任何一个建议??

I need to know a method by which i can know Pen drice instance handler id.
camn any one suggest??

推荐答案

正如我所说,通过 System.Management 很容易做到。



当然,没有这样的像拇指驱动器这样的概念。有一个可移动驱动器的概念。这是一个 DriveType 的驱动器.2。您需要扫描所有可用卷并找出可移动驱动器或一组驱动器。然后,您可以卸载其中一个或多个。完成后,操作系统保证您可以安全地删除驱动器/卡,因为所有以前未提交的都没有提交。



您需要获取的管理对象是 Win32_Volume 。所有卷都是通过WMI查询 SELECT * FROM Win32_Volume WHERE ... 获得的。



也请请参阅此CodeProject文章:使用C#弹出USB磁盘 [ ^ ]。







回答后续问题:



您无法直接访问 Win32_Volume ,因为它将是.NET类。 WMI为您提供反射界面。 (不要与.NET反射混淆。)你可以获取属性和方法我的名字并调用它们(不幸的是,这就是它的完成方式)。



例如,以下是如何拆卸可移动驱动器:

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 =
                   item.InvokeMethod("Dismount",
                       new object[] { false, false });
                if (result != 0) {/* show error */}
            } // loop





您可以获得 Win32_Volume 单独,来自C ++ API并通过其记录的名称调用方法/属性。



-SA



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

—SA

这篇关于在c#中获取Pendrive实例处理程序ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆