如何使用C#来获得个人电脑的所有驱动器与.NET个人电脑、驱动器、如何使用、NET

2023-09-03 03:26:16 作者:欧美大全

如何获得在PC中的所有驱动器。和每个类型的每个驱动器和自由空间的

how to get all drives in a PC. and types of every drive and free-space of each

即。系统驱动器,CD驱动器,DVD驱动器,可移动,...等。

i.e. System-Drive, CD-Drive, DVD-Drive, Removable, ... etc.

如果系统上安装有一个新的驱动器笔式驱动器或外部硬盘

If a system is attached with a new drive may be a pen drive or external hard disc.

如何探测到它们在附件?时间

推荐答案

要获得驱动器的列表,你可以使用的 System.IO.DriveInfo 类:

To get a list of the drives, you can use the System.IO.DriveInfo class:

foreach(var drive in DriveInfo.GetDrives())
{
    Console.WriteLine("Drive Type: {0}", drive.DriveType);
    Console.WriteLine("Drive Size: {0}", drive.TotalSize);
    Console.WriteLine("Drive Free Space: {0}", drive.TotalFreeSpace);
}

不幸的是,这并不提供一种方式来监听USB钥匙的插入。还有另外一个问题处理,你可以看看:

Unfortunately, this doesn't provide a way to listen for USB Key Insertions. There is another question dealing with that you could check out:

.NET - 检测USB驱动器插入和移除...