6
6
using Files . View_Models ;
7
7
using Files . Views ;
8
8
using Microsoft . Toolkit . Uwp . Extensions ;
9
+ using Microsoft . Toolkit . Uwp . Helpers ;
9
10
using Microsoft . Toolkit . Uwp . UI ;
10
11
using System ;
11
12
using System . Collections . Generic ;
@@ -610,7 +611,7 @@ public async void RapidAddItemsToCollectionAsync(string path)
610
611
// simply drop this instance
611
612
await semaphoreSlim . WaitAsync ( _semaphoreCTS . Token ) ;
612
613
}
613
- catch ( OperationCanceledException )
614
+ catch ( Exception ex ) when ( ex is OperationCanceledException || ex is ObjectDisposedException )
614
615
{
615
616
return ;
616
617
}
@@ -877,19 +878,19 @@ await DialogDisplayHelper.ShowDialog(
877
878
{
878
879
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS . FindExInfoBasic ;
879
880
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH ;
880
-
881
881
IntPtr hFile = FindFirstFileExFromApp ( path + "\\ *.*" , findInfoLevel , out WIN32_FIND_DATA findData , FINDEX_SEARCH_OPS . FindExSearchNameMatch , IntPtr . Zero ,
882
882
additionalFlags ) ;
883
- FileTimeToSystemTime ( ref findData . ftLastWriteTime , out SYSTEMTIME systemTimeOutput ) ;
884
- var itemDate = new DateTime (
885
- systemTimeOutput . Year ,
886
- systemTimeOutput . Month ,
887
- systemTimeOutput . Day ,
888
- systemTimeOutput . Hour ,
889
- systemTimeOutput . Minute ,
890
- systemTimeOutput . Second ,
891
- systemTimeOutput . Milliseconds ,
892
- DateTimeKind . Utc ) ;
883
+
884
+ DateTime itemDate = DateTime . UtcNow ;
885
+ try
886
+ {
887
+ FileTimeToSystemTime ( ref findData . ftLastWriteTime , out SYSTEMTIME systemTimeOutput ) ;
888
+ itemDate = new DateTime (
889
+ systemTimeOutput . Year , systemTimeOutput . Month , systemTimeOutput . Day ,
890
+ systemTimeOutput . Hour , systemTimeOutput . Minute , systemTimeOutput . Second , systemTimeOutput . Milliseconds ,
891
+ DateTimeKind . Utc ) ;
892
+ }
893
+ catch ( ArgumentException ) { }
893
894
894
895
CurrentFolder = new ListedItem ( _rootFolder . FolderRelativeId , returnformat )
895
896
{
@@ -1109,27 +1110,27 @@ private void WatchForDirectoryChanges(string path)
1109
1110
{
1110
1111
case FILE_ACTION_ADDED :
1111
1112
Debug . WriteLine ( "File " + FileName + " added to working directory." ) ;
1112
- AddFileOrFolder ( FileName , returnformat ) ;
1113
+ AddFileOrFolder ( FileName , returnformat ) . GetAwaiter ( ) . GetResult ( ) ;
1113
1114
break ;
1114
1115
1115
1116
case FILE_ACTION_REMOVED :
1116
1117
Debug . WriteLine ( "File " + FileName + " removed from working directory." ) ;
1117
- RemoveFileOrFolder ( FileName ) ;
1118
+ RemoveFileOrFolder ( FileName ) . GetAwaiter ( ) . GetResult ( ) ;
1118
1119
break ;
1119
1120
1120
1121
case FILE_ACTION_MODIFIED :
1121
1122
Debug . WriteLine ( "File " + FileName + " had attributes modified in the working directory." ) ;
1122
- UpdateFileOrFolder ( FilesAndFolders . ToList ( ) . First ( x => x . ItemPath . Equals ( FileName ) ) ) ;
1123
+ UpdateFileOrFolder ( FileName ) . GetAwaiter ( ) . GetResult ( ) ;
1123
1124
break ;
1124
1125
1125
1126
case FILE_ACTION_RENAMED_OLD_NAME :
1126
1127
Debug . WriteLine ( "File " + FileName + " will be renamed in the working directory." ) ;
1127
- RemoveFileOrFolder ( FileName ) ;
1128
+ RemoveFileOrFolder ( FileName ) . GetAwaiter ( ) . GetResult ( ) ;
1128
1129
break ;
1129
1130
1130
1131
case FILE_ACTION_RENAMED_NEW_NAME :
1131
1132
Debug . WriteLine ( "File " + FileName + " was renamed in the working directory." ) ;
1132
- AddFileOrFolder ( FileName , returnformat ) ;
1133
+ AddFileOrFolder ( FileName , returnformat ) . GetAwaiter ( ) . GetResult ( ) ;
1133
1134
break ;
1134
1135
1135
1136
default :
@@ -1233,13 +1234,13 @@ public void AddFileOrFolderFromShellFile(ShellFileItem item, string dateReturnFo
1233
1234
UpdateDirectoryInfo ( ) ;
1234
1235
}
1235
1236
1236
- public void AddFileOrFolder ( ListedItem item )
1237
+ private void AddFileOrFolder ( ListedItem item )
1237
1238
{
1238
1239
_filesAndFolders . Add ( item ) ;
1239
1240
IsFolderEmptyTextDisplayed = false ;
1240
1241
}
1241
1242
1242
- private void AddFileOrFolder2 ( string fileOrFolderPath , string dateReturnFormat )
1243
+ private async Task AddFileOrFolder ( string fileOrFolderPath , string dateReturnFormat )
1243
1244
{
1244
1245
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS . FindExInfoBasic ;
1245
1246
int additionalFlags = FIND_FIRST_EX_CASE_SENSITIVE ;
@@ -1248,32 +1249,18 @@ private void AddFileOrFolder2(string fileOrFolderPath, string dateReturnFormat)
1248
1249
additionalFlags ) ;
1249
1250
FindClose ( hFile ) ;
1250
1251
1251
- if ( ( findData . dwFileAttributes & 0x10 ) > 0 ) // FILE_ATTRIBUTE_DIRECTORY
1252
- {
1253
- AddFolder ( findData , Directory . GetParent ( fileOrFolderPath ) . FullName , dateReturnFormat ) ;
1254
- }
1255
- else
1252
+ await CoreApplication . MainView . ExecuteOnUIThreadAsync ( ( ) =>
1256
1253
{
1257
- AddFile ( findData , Directory . GetParent ( fileOrFolderPath ) . FullName , dateReturnFormat ) ;
1258
- }
1259
- }
1260
-
1261
- private async void AddFileOrFolder ( string path , string dateReturnFormat )
1262
- {
1263
- await CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( CoreDispatcherPriority . Normal ,
1264
- ( ) =>
1254
+ if ( ( findData . dwFileAttributes & 0x10 ) > 0 ) // FILE_ATTRIBUTE_DIRECTORY
1265
1255
{
1266
- try
1267
- {
1268
- AddFileOrFolder2 ( path , dateReturnFormat ) ;
1269
- }
1270
- catch ( Exception )
1271
- {
1272
- // Ignore this..
1273
- }
1274
-
1275
- UpdateDirectoryInfo ( ) ;
1276
- } ) ;
1256
+ AddFolder ( findData , Directory . GetParent ( fileOrFolderPath ) . FullName , dateReturnFormat ) ;
1257
+ }
1258
+ else
1259
+ {
1260
+ AddFile ( findData , Directory . GetParent ( fileOrFolderPath ) . FullName , dateReturnFormat ) ;
1261
+ }
1262
+ UpdateDirectoryInfo ( ) ;
1263
+ } ) ;
1277
1264
}
1278
1265
1279
1266
private void UpdateDirectoryInfo ( )
@@ -1291,7 +1278,7 @@ private void UpdateDirectoryInfo()
1291
1278
}
1292
1279
}
1293
1280
1294
- public async void UpdateFileOrFolder ( ListedItem item )
1281
+ private async Task UpdateFileOrFolder ( ListedItem item )
1295
1282
{
1296
1283
IStorageItem storageItem = null ;
1297
1284
if ( item . PrimaryItemAttribute == StorageItemTypes . File )
@@ -1305,36 +1292,43 @@ public async void UpdateFileOrFolder(ListedItem item)
1305
1292
if ( storageItem != null )
1306
1293
{
1307
1294
var syncStatus = await CheckCloudDriveSyncStatus ( storageItem ) ;
1308
- await CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( CoreDispatcherPriority . Normal ,
1309
- ( ) =>
1310
- {
1311
- item . SyncStatusUI = CloudDriveSyncStatusUI . FromCloudDriveSyncStatus ( syncStatus ) ;
1312
- } ) ;
1295
+ await CoreApplication . MainView . ExecuteOnUIThreadAsync ( ( ) =>
1296
+ {
1297
+ item . SyncStatusUI = CloudDriveSyncStatusUI . FromCloudDriveSyncStatus ( syncStatus ) ;
1298
+ } ) ;
1313
1299
}
1314
1300
}
1315
1301
1316
- public async void RemoveFileOrFolder ( ListedItem item )
1302
+ private async Task UpdateFileOrFolder ( string path )
1317
1303
{
1318
- await CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( CoreDispatcherPriority . Normal ,
1319
- ( ) =>
1304
+ var matchingItem = FilesAndFolders . ToList ( ) . FirstOrDefault ( x => x . ItemPath . Equals ( path ) ) ;
1305
+ if ( matchingItem != null )
1306
+ {
1307
+ await UpdateFileOrFolder ( matchingItem ) ;
1308
+ }
1309
+ }
1310
+
1311
+ public async Task RemoveFileOrFolder ( ListedItem item )
1312
+ {
1313
+ await CoreApplication . MainView . ExecuteOnUIThreadAsync ( ( ) =>
1314
+ {
1315
+ _filesAndFolders . Remove ( item ) ;
1316
+ if ( _filesAndFolders . Count == 0 )
1320
1317
{
1321
- _filesAndFolders . Remove ( item ) ;
1322
- if ( _filesAndFolders . Count == 0 )
1323
- {
1324
- IsFolderEmptyTextDisplayed = true ;
1325
- }
1326
- App . JumpList . RemoveFolder ( item . ItemPath ) ;
1318
+ IsFolderEmptyTextDisplayed = true ;
1319
+ }
1320
+ App . JumpList . RemoveFolder ( item . ItemPath ) ;
1327
1321
1328
- UpdateDirectoryInfo ( ) ;
1329
- } ) ;
1322
+ UpdateDirectoryInfo ( ) ;
1323
+ } ) ;
1330
1324
}
1331
1325
1332
- public void RemoveFileOrFolder ( string path )
1326
+ public async Task RemoveFileOrFolder ( string path )
1333
1327
{
1334
1328
var matchingItem = FilesAndFolders . ToList ( ) . FirstOrDefault ( x => x . ItemPath . Equals ( path ) ) ;
1335
1329
if ( matchingItem != null )
1336
1330
{
1337
- RemoveFileOrFolder ( matchingItem ) ;
1331
+ await RemoveFileOrFolder ( matchingItem ) ;
1338
1332
}
1339
1333
}
1340
1334
@@ -1346,16 +1340,20 @@ private void AddFolder(WIN32_FIND_DATA findData, string pathRoot, string dateRet
1346
1340
return ;
1347
1341
}
1348
1342
1349
- FileTimeToSystemTime ( ref findData . ftLastWriteTime , out SYSTEMTIME systemTimeOutput ) ;
1350
- var itemDate = new DateTime (
1351
- systemTimeOutput . Year ,
1352
- systemTimeOutput . Month ,
1353
- systemTimeOutput . Day ,
1354
- systemTimeOutput . Hour ,
1355
- systemTimeOutput . Minute ,
1356
- systemTimeOutput . Second ,
1357
- systemTimeOutput . Milliseconds ,
1358
- DateTimeKind . Utc ) ;
1343
+ DateTime itemDate ;
1344
+ try
1345
+ {
1346
+ FileTimeToSystemTime ( ref findData . ftLastWriteTime , out SYSTEMTIME systemTimeOutput ) ;
1347
+ itemDate = new DateTime (
1348
+ systemTimeOutput . Year , systemTimeOutput . Month , systemTimeOutput . Day ,
1349
+ systemTimeOutput . Hour , systemTimeOutput . Minute , systemTimeOutput . Second , systemTimeOutput . Milliseconds ,
1350
+ DateTimeKind . Utc ) ;
1351
+ }
1352
+ catch ( ArgumentException )
1353
+ {
1354
+ // Invalid date means invalid findData, do not add to list
1355
+ return ;
1356
+ }
1359
1357
var itemPath = Path . Combine ( pathRoot , findData . cFileName ) ;
1360
1358
1361
1359
_filesAndFolders . Add ( new ListedItem ( null , dateReturnFormat )
@@ -1399,23 +1397,32 @@ private void AddFile(WIN32_FIND_DATA findData, string pathRoot, string dateRetur
1399
1397
}
1400
1398
}
1401
1399
1402
- FileTimeToSystemTime ( ref findData . ftLastWriteTime , out SYSTEMTIME systemModifiedDateOutput ) ;
1403
- var itemModifiedDate = new DateTime (
1404
- systemModifiedDateOutput . Year , systemModifiedDateOutput . Month , systemModifiedDateOutput . Day ,
1405
- systemModifiedDateOutput . Hour , systemModifiedDateOutput . Minute , systemModifiedDateOutput . Second , systemModifiedDateOutput . Milliseconds ,
1406
- DateTimeKind . Utc ) ;
1400
+ DateTime itemModifiedDate , itemCreatedDate , itemLastAccessDate ;
1401
+ try
1402
+ {
1403
+ FileTimeToSystemTime ( ref findData . ftLastWriteTime , out SYSTEMTIME systemModifiedDateOutput ) ;
1404
+ itemModifiedDate = new DateTime (
1405
+ systemModifiedDateOutput . Year , systemModifiedDateOutput . Month , systemModifiedDateOutput . Day ,
1406
+ systemModifiedDateOutput . Hour , systemModifiedDateOutput . Minute , systemModifiedDateOutput . Second , systemModifiedDateOutput . Milliseconds ,
1407
+ DateTimeKind . Utc ) ;
1407
1408
1408
- FileTimeToSystemTime ( ref findData . ftCreationTime , out SYSTEMTIME systemCreatedDateOutput ) ;
1409
- var itemCreatedDate = new DateTime (
1410
- systemCreatedDateOutput . Year , systemCreatedDateOutput . Month , systemCreatedDateOutput . Day ,
1411
- systemCreatedDateOutput . Hour , systemCreatedDateOutput . Minute , systemCreatedDateOutput . Second , systemCreatedDateOutput . Milliseconds ,
1412
- DateTimeKind . Utc ) ;
1409
+ FileTimeToSystemTime ( ref findData . ftCreationTime , out SYSTEMTIME systemCreatedDateOutput ) ;
1410
+ itemCreatedDate = new DateTime (
1411
+ systemCreatedDateOutput . Year , systemCreatedDateOutput . Month , systemCreatedDateOutput . Day ,
1412
+ systemCreatedDateOutput . Hour , systemCreatedDateOutput . Minute , systemCreatedDateOutput . Second , systemCreatedDateOutput . Milliseconds ,
1413
+ DateTimeKind . Utc ) ;
1413
1414
1414
- FileTimeToSystemTime ( ref findData . ftLastAccessTime , out SYSTEMTIME systemLastAccessOutput ) ;
1415
- var itemLastAccessDate = new DateTime (
1416
- systemLastAccessOutput . Year , systemLastAccessOutput . Month , systemLastAccessOutput . Day ,
1417
- systemLastAccessOutput . Hour , systemLastAccessOutput . Minute , systemLastAccessOutput . Second , systemLastAccessOutput . Milliseconds ,
1418
- DateTimeKind . Utc ) ;
1415
+ FileTimeToSystemTime ( ref findData . ftLastAccessTime , out SYSTEMTIME systemLastAccessOutput ) ;
1416
+ itemLastAccessDate = new DateTime (
1417
+ systemLastAccessOutput . Year , systemLastAccessOutput . Month , systemLastAccessOutput . Day ,
1418
+ systemLastAccessOutput . Hour , systemLastAccessOutput . Minute , systemLastAccessOutput . Second , systemLastAccessOutput . Milliseconds ,
1419
+ DateTimeKind . Utc ) ;
1420
+ }
1421
+ catch ( ArgumentException )
1422
+ {
1423
+ // Invalid date means invalid findData, do not add to list
1424
+ return ;
1425
+ }
1419
1426
1420
1427
long itemSizeBytes = findData . GetSize ( ) ;
1421
1428
var itemSize = ByteSize . FromBytes ( itemSizeBytes ) . ToBinaryString ( ) . ConvertSizeAbbreviation ( ) ;
0 commit comments