无法在 C# 应用程序中检测到硬盘驱动器
Not able to detect Hard disk drive in C# application
我写了一个 api 来将所有外部存储驱动器连接到我的计算机。但是我无法检测到硬盘驱动器。
private async Task GetAllDrivesConnected()
{
await Task.Run(() =>
{
var drives = DriveInfo.GetDrives();
lock (this.myUsbDriveListLock)
{
foreach (var aDrive in drives .Where(theDrive => theDrive.DriveType == DriveType.Removable && theDrive.IsReady))
{
DriveLetter_Array.Add(aDrive.Name);
}
}
string LatestPath = DriveLetter_Array.LastOrDefault();
this.SetCurrentUsbPath(LatestPath);
var DeviceMessageEventArgs = new DeviceMessageEventArgs { Drive = LatestPath, UsbAdded = true };
FireEventAddPathRemovePath(DeviceMessageEventArgs);
});
}
我正在使用 WML 查询来监听设备插入事件:
private async Task DeviceNotification()
{
await Task.Run(() =>
{
var InsertQuery = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
myInsertionWatcher = new ManagementEventWatcher(InsertQuery);
myInsertionWatcher.EventArrived += this.DeviceInsertedEvent;
myInsertionWatcher.Start();
var RemoveQuery = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 3");
myRemovalWatcher = new ManagementEventWatcher(RemoveQuery);
myRemovalWatcher.EventArrived += this.DeviceRemovedEvent;
myRemovalWatcher.Start();
});
}
请尝试在启动后添加等待下一个事件的方法
myInsertionWatcher.WaitForNextEvent();
如果您真的检查了其他 DriveType
,您的评论并不清楚。如果我 运行 在连接外部硬盘驱动器后我得到以下图像的输出:
DriveInfo.GetDrives().Where(d => d.IsReady)
如您所见,它有 DriveType
Fixed
,即使它是通过 USB 端口连接的。
您可能想要更改 foreach
循环:
foreach (var aDrive in drives .Where(theDrive => theDrive.IsReady))
{
DriveLetter_Array.Add(aDrive.Name);
}
如果您只想查看外部硬盘驱动器,则可能需要查看 this answer
我写了一个 api 来将所有外部存储驱动器连接到我的计算机。但是我无法检测到硬盘驱动器。
private async Task GetAllDrivesConnected()
{
await Task.Run(() =>
{
var drives = DriveInfo.GetDrives();
lock (this.myUsbDriveListLock)
{
foreach (var aDrive in drives .Where(theDrive => theDrive.DriveType == DriveType.Removable && theDrive.IsReady))
{
DriveLetter_Array.Add(aDrive.Name);
}
}
string LatestPath = DriveLetter_Array.LastOrDefault();
this.SetCurrentUsbPath(LatestPath);
var DeviceMessageEventArgs = new DeviceMessageEventArgs { Drive = LatestPath, UsbAdded = true };
FireEventAddPathRemovePath(DeviceMessageEventArgs);
});
}
我正在使用 WML 查询来监听设备插入事件:
private async Task DeviceNotification()
{
await Task.Run(() =>
{
var InsertQuery = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
myInsertionWatcher = new ManagementEventWatcher(InsertQuery);
myInsertionWatcher.EventArrived += this.DeviceInsertedEvent;
myInsertionWatcher.Start();
var RemoveQuery = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 3");
myRemovalWatcher = new ManagementEventWatcher(RemoveQuery);
myRemovalWatcher.EventArrived += this.DeviceRemovedEvent;
myRemovalWatcher.Start();
});
}
请尝试在启动后添加等待下一个事件的方法
myInsertionWatcher.WaitForNextEvent();
如果您真的检查了其他 DriveType
,您的评论并不清楚。如果我 运行 在连接外部硬盘驱动器后我得到以下图像的输出:
DriveInfo.GetDrives().Where(d => d.IsReady)
如您所见,它有 DriveType
Fixed
,即使它是通过 USB 端口连接的。
您可能想要更改 foreach
循环:
foreach (var aDrive in drives .Where(theDrive => theDrive.IsReady))
{
DriveLetter_Array.Add(aDrive.Name);
}
如果您只想查看外部硬盘驱动器,则可能需要查看 this answer