概要
cscの作法、調べてみた。
ドライブを表示するアプリ見つけたので、やってみた。
サンプルコード
using System;
using System.Linq;
using System.Management;
using System.Collections.Generic;
using System.Windows;
using System.IO;
public class USBUtil {
public static bool EsUnidadExterna(string elPath) {
var disco = Path.GetPathRoot(Path.GetFullPath(elPath));
var usbD = GetUsbDriveLetters();
return usbD.Contains(disco);
}
public static List<string> GetUsbDriveLetters() {
var usbDrivesLetters = from drive in new ManagementObjectSearcher("select * from Win32_DiskDrive WHERE MediaType like '%External%' OR InterfaceType='USB'").Get().Cast<ManagementObject>()
from o in drive.GetRelated("Win32_DiskPartition").Cast<ManagementObject>()
from i in o.GetRelated("Win32_LogicalDisk").Cast<ManagementObject>()
select string.Format("{0}\\", i["Name"]);
return usbDrivesLetters.ToList();
}
}
class Program {
static void Main(string[] args) {
foreach (var dr in DriveInfo.GetDrives())
Console.WriteLine("Unidad: {0}, tipo: {1}, label: {2}, fmt: {3}", dr.Name, dr.DriveType, dr.VolumeLabel, dr.DriveFormat);
Console.WriteLine();
var usb = USBUtil.GetUsbDriveLetters();
foreach (var s in usb)
Console.WriteLine("{0}", s);
Console.WriteLine();
}
}
実行結果
>usbu0
Unidad: C:\, tipo: Fixed, label: Windows, fmt: NTFS
Unidad: D:\, tipo: Removable, label: , fmt: FAT32
D:\
以上。