C#检测移动硬盘并获取移动硬盘盘符的方法

网上找了很久关于C#检测移动硬盘并获取盘符的代码但没能找到,所以只能自己解决了

C#获取所有硬盘

var arr = DriveInfo.GetDrives();

得出的所有磁盘,发现对于移动硬盘,DriveType 不是 Removable 类型,而是 Fixed 枚举类型。

C#检测移动硬盘,网上找了很久,没有现成正确的代码,只有自己想办法了。

代码如下:

public static List<string> GetListDisk()
    {
      List<string> lstDisk = new List<string>();
      ManagementClass mgtCls = new ManagementClass("Win32_DiskDrive");
      var disks = mgtCls.GetInstances();
      foreach (ManagementObject mo in disks)
      {
        //if (mo.Properties["InterfaceType"].Value.ToString() != "SCSI"
        //  && mo.Properties["InterfaceType"].Value.ToString() != "USB"
        //  )
        //  continue;

        if (mo.Properties["MediaType"].Value == null ||
          mo.Properties["MediaType"].Value.ToString() != "External hard disk media")
        {
          continue;
        }

        //foreach (var prop in mo.Properties)
        //{
        //  Console.WriteLine(prop.Name + "\t" + prop.Value);
        //}

        foreach (ManagementObject diskPartition in mo.GetRelated("Win32_DiskPartition"))
        {
          foreach (ManagementBaseObject disk in diskPartition.GetRelated("Win32_LogicalDisk"))
          {
            lstDisk.Add(disk.Properties["Name"].Value.ToString());
          }
        }

        //Console.WriteLine("-------------------------------------------------------------------------------------------");
      }
      return lstDisk;
    }

此代码是通过找 Win32_DiskDrive,Win32_DiskPartition,Win32_LogicalDisk 对应的属性值的规律, 三个之间的关系 得出 移动硬盘的盘符的。

(0)

相关推荐

  • c#检测usb设备拨插类库USBClassLibrary分享

    复制代码 代码如下: private void USBPort_USBDeviceAttached(objectsender,USBClass.USBDeviceEventArgs e){if (!MyUSBDeviceConnected){if(USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID,ref USBDeviceProperties, false)){ //My Device is connectedMyUSBDeviceConnected

  • C#检测是否有u盘插入的方法

    本文实例讲述了C#检测是否有u盘插入的方法.分享给大家供大家参考.具体如下: 该C#代码可监控是否有u盘插入,同时可以监控其它驱动器的变化 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Ru

  • C#利用win32 Api 修改本地系统时间、获取硬盘序列号

    C#利用win32 Api 修改本地系统时间.获取硬盘序列号,可以用于软件注册机制的编写! 复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace Fengyun {     public class Win32     {         #region 修改本地系统时间         [DllIm

  • C#实现读取指定盘符硬盘序列号的方法

    本文实例讲述了C#实现读取指定盘符硬盘序列号的方法.分享给大家供大家参考,具体如下: using System; using System.IO; using System.Runtime.InteropServices; using System.Text; using Microsoft.Win32; namespace Wjb.ReadOrWriteIniAndReg { /**/////// 读取指定盘符的硬盘序列号 /// public class HardDiskVal { [Dll

  • C#获取机器码的方法详解(机器名,CPU编号,硬盘编号,网卡mac等)

    本文实例讲述了C#获取机器码的方法.分享给大家供大家参考,具体如下: using System.Runtime.InteropServices; using System.Management; using System; public class HardwareInfo { //取机器名 public string GetHostName() { return System.Net.Dns.GetHostName(); } //取CPU编号 public String GetCpuID()

  • 用C#获取硬盘序列号,CPU序列号,网卡MAC地址的源码

    privatestring[]GetMoc() { string[]str=newstring[3]; ManagementClassmcCpu=newManagementClass("win32_Processor"); ManagementObjectCollectionmocCpu=mcCpu.GetInstances(); foreach(ManagementObjectminmocCpu) { str[0]=m["ProcessorId"].ToStrin

  • C#获取硬盘序列号的问题小结

    先给大家描述下问题的来龙去脉. 具体问题是这样的:我用下面这段获取硬盘型信息的代码做成的exe文件,在机子上测试的时候,出现直接双击运行和用管理员身份运行结果不一样的情况,这个问题该怎么解决? public static String GetHardWareId() { String num = null; List<String> hdids = new List<string>(); ManagementClass mc = new ManagementClass("

  • C#获取硬盘编号的方法

    本文实例讲述了C#获取硬盘编号的方法.分享给大家供大家参考.具体实现方法如下: ManagementClass mc = new ManagementClass("Win32_PhysicalMedia"); //Win32_DiskDrive不包含SerialNumber属性. ManagementObjectCollection moc = mc.GetInstances(); string strID = null ; foreach( ManagementObject mo i

  • C#检测pc光驱里是否插入了光盘的方法

    本文实例讲述了C#检测pc光驱里是否插入了光盘的方法.分享给大家供大家参考.具体如下: C# 检测pc光驱里是否插入了光盘,需要添加System.Management.dll 的引用 using System; using System.Management; namespace CDROMManagement { class WMIEvent { static void Main(string[] args) { WMIEvent we = new WMIEvent(); Management

  • 批处理bat命令 获取当前盘符和当前目录和上级目录的代码

    批处理命令获取当前盘符和当前目录 当前盘符:%~d0 当前路径:%cd% 当前执行命令行:%0 当前bat文件路径:%~dp0 当前bat文件短路径:%~sdp0 测试 echo 当前盘符:%~d0 echo 当前路径:%cd% echo 当前执行命令行:%0 echo 当前bat文件路径:%~dp0 echo 当前bat文件短路径:%~sdp0 效果如下图所示 可以用echo %cd%进行打印测试 windows 的快捷方式不能使用相对路径.可以用批处理文件获取相对路径. set pa=%cd

  • jsp编程获取当前目录下的文件和目录及windows盘符的方法

    本文实例讲述了jsp编程获取当前目录下的文件和目录及windows盘符的方法.分享给大家供大家参考,具体如下: (一)获取当前目录下的文件和目录 知识点 1 file对象的应用 2 listFiles()方法 3 isDirectory()方法,isFile()方法 判断是否为目录或是文件 4转换字符串方法toString (1)创建一个file对象dir, 然后用listFiles()方法返回当前目录下所有文件 String path=request.getRealPath("/")

  • C#获取U盘序列号的方法

    本文实例讲述了C#获取U盘序列号的方法.分享给大家供大家参考.具体如下: using System.Management; private List<string> _serialNumber = new List<string>(); /// <summary> /// 调用这个函数将本机所有U盘序列号存储到_serialNumber中 /// </summary> private void matchDriveLetterWithSerial() { s

  • C#检测移动硬盘并获取移动硬盘盘符的方法

    网上找了很久关于C#检测移动硬盘并获取盘符的代码但没能找到,所以只能自己解决了 C#获取所有硬盘 var arr = DriveInfo.GetDrives(); 得出的所有磁盘,发现对于移动硬盘,DriveType 不是 Removable 类型,而是 Fixed 枚举类型. C#检测移动硬盘,网上找了很久,没有现成正确的代码,只有自己想办法了. 代码如下: public static List<string> GetListDisk() { List<string> lstDi

  • Powershell中获取所有磁盘盘符的方法

    快速获取当前电脑所有磁盘: 复制代码 代码如下: #requires -Version 1 [Environment]::GetLogicalDrives() 结果如下: 复制代码 代码如下: PS> C:\ D:\ E:\ F:\ G:\

  • Vista下双击盘符不能打开的处理技巧

    Windows Vista操作系统中你遇到过双击盘符不能打开的问题了吗?下面我们就告诉大家解决方法. 一.打开"文件"类型的文件需要该程序 键入要使用的可执行文件:C: 说明:这类病情症状并不是唯一的,无法找到的内容可能不一样.有时可能是"Windows无法找到setup.exe"或显示"控制面版". 这是因为病毒在驱动器下面写入了一个AutoRun.inf文件. 解决方法:(以D盘为例): 开始---运行---cmd(打开命令提示符) (也可以

  • 用批处理实现映射盘网络盘为固定盘符,请更改冲突的硬盘盘符

    以前在CSDN上提交的FAQ,不能通过搜索引擎来搜索CSDN的FAQ,找了好一会才找到.现CSDN的FAQ只有CSDN用户才能正常访问,固转到BLOG. http://faq.csdn.net/FAQUnfurl.aspx?id=216232 Q:现有50台电脑需要映射一网络驱动,网络映射盘必须为F盘.问题是目前有部分电脑已有本地F盘,需要把本地的F盘的盘符更换成J后,再映射网络驱动到F盘.由于电脑多,故想用批处理文件解决. A:---------------------------------

  • 自动清除电脑垃圾及删除windows默认共享盘符的批处理bat

    by:zuifeng258Windows在默认情况下几个盘多是共享的,它们是隐藏的危险. 在dos下用命令"net share"可以查看... 不能截图,就只能打打字了 复制代码 代码如下: @echo off echo 正在自动删除admin$管理共享和ipc$管道共享, net share admin$ /del net share IPC$ /del net share C$ /del net share D$ /del net share E$ /del net share F

  • iOS 获取设备唯一标示符的方法详解

    在开发中会遇到应用需要记录设备标示,即使应用卸载后再安装也可重新识别的情况,在这写一种实现方式--读取设备的UUID(Universally Unique Identifier)并通过KeyChain记录. 首先iOS中获取设备唯一标示符的方法一直随版本的更新而变化.iOS 2.0版本以后UIDevice提供一个获取设备唯一标识符的方法uniqueIdentifier,通过该方法我们可以获取设备的序列号,这个也是目前为止唯一可以确认唯一的标示符.好景不长,因为该唯一标识符与手机一一对应,苹果觉得

随机推荐