首先,很可能我以错误的方式处理问题,在这种情况下,我很乐意接受替代方案。
我想要实现的是检测 USB 设备连接到计算机后创建的驱动器。
以下是简化的工作流程:
// Get list of removable drives before user connects the USB cable
List<string> listRemovableDrivesBefore = GetRemovableDriveList();
// Tell user to connect USB cable
...
// Start listening for a connection of a USB device
...
// Loop until device is connected or time runs out
do
{
...
} while
// Get list of removable drives after USB device is connected
List<string> listRemovableDrivesAfter = GetRemovableDriveList();
// Find out which drive was created after USB has been connected
???
GetRemovableDriveList
返回可移动驱动器盘符的字符串列表。
我的想法是在连接设备之前获取可移动驱动器的列表,在连接设备之后获取另一个列表,然后从列表中删除第一个列表的内容其次,我将留下刚刚连接的驱动器(通常只有一个)。
但我找不到一种从一个列表中“减去”另一个列表的简单方法。任何人都可以提出一个解决方案,甚至是实现我想要做的事情的更好方法。
注意:项目面向 .NET Framework 2.0,因此不可能使用 LINQ。
谢谢!
First of all, it very well could be that I'm approaching my problem the wrong way, in which case I'd gladly accept alternatives.
What I'm trying to achieve is to detect which drive was created after a USB device has been connected to a computer.
Here is the simplified workflow:
// Get list of removable drives before user connects the USB cable
List<string> listRemovableDrivesBefore = GetRemovableDriveList();
// Tell user to connect USB cable
...
// Start listening for a connection of a USB device
...
// Loop until device is connected or time runs out
do
{
...
} while
// Get list of removable drives after USB device is connected
List<string> listRemovableDrivesAfter = GetRemovableDriveList();
// Find out which drive was created after USB has been connected
???
GetRemovableDriveList
returns a list of strings of the removable drive letters.
My idea was to get a list of removable drives before the device is connected, and another list after it is connected, and that by removing the contents of the first list from the second, I would be left with drives that were just connected (normally only one).
But I can't find a simple way of "subtracting" one list from another. Anyone could suggest a solution, or even a better way of achieving what I'm trying to do.
Note: project is targeting the .NET framework 2.0, so no LINQ possible.
Thanks!
发布评论
评论(3)
执行此操作的一般方法是将源集合中的所有项目添加到字典中,然后删除其他集合中的项目:
A general way to do this would be to add all the items from the source collection to a dictionary and then remove items in the other collection:
对于少量元素,则使用 foreach 循环rel="nofollow noreferrer">
Contains
调用应该可以解决问题:如果集合有很多元素,那么您可以使用
字典
而不是列表
< /a>. (理想情况下,您应该使用HashSet
,但这在框架的版本 2 中不可用。)For a small number of elements then a
foreach
loop with aContains
call should do the trick:If the collection has many elements then you could make the lookups more efficient by using a
Dictionary<K,V>
rather than aList<T>
. (Ideally you'd use aHashSet<T>
, but that's not available in version 2 of the framework.)您可以使用 Linq 扩展方法的 Subtract 和 Insersect,就像使用数学集一样。
A = 原始。
B = 之后。
A -(A 与 B 相交)= 从原始内容中删除
B - (A 插入 B) = new
var intersect = A.Intersect(B);
var 删除 = A.Substract(相交);
var new = B.Substract(intersect)
希望这对您有用。
You can work with Linq extension method's Subtract and Insersect, same as you do with math set.
A = Original.
B = After.
A - (A Intersect B) = removed from original
B - (A insersect B) = new
var intersect = A.Intersect(B);
var removed = A.Substract(intersect);
var new = B.Substract(intersect)
Hope this works for you.