@@ -24,22 +24,28 @@ public class DrivesManager
24
24
25
25
public DrivesManager ( )
26
26
{
27
+ Task findDrivesTask = null ;
27
28
try
28
29
{
29
- GetDrives ( Drives ) ;
30
- GetVirtualDrivesList ( Drives ) ;
30
+ findDrivesTask = GetDrives ( Drives ) ;
31
31
}
32
32
catch ( AggregateException e )
33
33
{
34
34
ShowUserConsentOnInit = true ;
35
35
}
36
36
37
- _deviceWatcher = DeviceInformation . CreateWatcher ( StorageDevice . GetDeviceSelector ( ) ) ;
38
- _deviceWatcher . Added += DeviceAdded ;
39
- _deviceWatcher . Removed += DeviceRemoved ;
40
- _deviceWatcher . Updated += DeviceUpdated ;
41
- _deviceWatcher . EnumerationCompleted += DeviceWatcher_EnumerationCompleted ;
42
- _deviceWatcher . Start ( ) ;
37
+ findDrivesTask . ContinueWith ( ( x ) =>
38
+ {
39
+ GetVirtualDrivesList ( Drives ) ;
40
+
41
+ _deviceWatcher = DeviceInformation . CreateWatcher ( StorageDevice . GetDeviceSelector ( ) ) ;
42
+ _deviceWatcher . Added += DeviceAdded ;
43
+ _deviceWatcher . Removed += DeviceRemoved ;
44
+ _deviceWatcher . Updated += DeviceUpdated ;
45
+ _deviceWatcher . EnumerationCompleted += DeviceWatcher_EnumerationCompleted ;
46
+ _deviceWatcher . Start ( ) ;
47
+ } ) ;
48
+
43
49
}
44
50
45
51
private async void DeviceWatcher_EnumerationCompleted ( DeviceWatcher sender , object args )
@@ -87,7 +93,11 @@ private async void DeviceAdded(DeviceWatcher sender, DeviceInformation args)
87
93
// Update the collection on the ui-thread.
88
94
try
89
95
{
90
- CoreApplication . MainView . Dispatcher . RunAsync ( CoreDispatcherPriority . Low , ( ) => { Drives . Add ( driveItem ) ; } ) ;
96
+ CoreApplication . MainView . Dispatcher . RunAsync ( CoreDispatcherPriority . Low , ( ) =>
97
+ {
98
+ Drives . Add ( driveItem ) ;
99
+ DeviceWatcher_EnumerationCompleted ( null , null ) ;
100
+ } ) ;
91
101
}
92
102
catch ( Exception e )
93
103
{
@@ -110,7 +120,11 @@ private async void DeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate a
110
120
// Update the collection on the ui-thread.
111
121
try
112
122
{
113
- CoreApplication . MainView . Dispatcher . RunAsync ( CoreDispatcherPriority . Low , ( ) => { Drives . Remove ( drive ) ; } ) ;
123
+ CoreApplication . MainView . Dispatcher . RunAsync ( CoreDispatcherPriority . Low , ( ) =>
124
+ {
125
+ Drives . Remove ( drive ) ;
126
+ DeviceWatcher_EnumerationCompleted ( null , null ) ;
127
+ } ) ;
114
128
}
115
129
catch ( Exception e )
116
130
{
@@ -126,9 +140,20 @@ private void DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate args)
126
140
Debug . WriteLine ( "Devices updated" ) ;
127
141
}
128
142
129
- private void GetDrives ( IList < DriveItem > list )
143
+ private async Task GetDrives ( IList < DriveItem > list )
130
144
{
131
- var drives = DriveInfo . GetDrives ( ) ;
145
+ var drives = DriveInfo . GetDrives ( ) . ToList ( ) ;
146
+
147
+ var remDevices = await DeviceInformation . FindAllAsync ( StorageDevice . GetDeviceSelector ( ) ) ;
148
+ var supportedDevicesNames = remDevices . Select ( x => StorageDevice . FromId ( x . Id ) . Name ) ;
149
+ foreach ( DriveInfo driveInfo in drives . ToList ( ) )
150
+ {
151
+ if ( ! supportedDevicesNames . Contains ( driveInfo . Name ) && driveInfo . DriveType == System . IO . DriveType . Removable )
152
+ {
153
+ drives . Remove ( driveInfo ) ;
154
+ }
155
+ }
156
+
132
157
133
158
foreach ( var drive in drives )
134
159
{
0 commit comments