15
15
using Windows . ApplicationModel . AppService ;
16
16
using Windows . Foundation . Collections ;
17
17
using Windows . Storage ;
18
- using static Vanara . PInvoke . Shell32 ;
18
+ using Vanara . PInvoke ;
19
+ using NLog ;
19
20
20
21
namespace FilesFullTrust
21
22
{
22
23
internal class Program
23
24
{
24
- private static readonly NLog . Logger Logger = NLog . LogManager . GetCurrentClassLogger ( ) ;
25
+ private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
25
26
26
27
[ STAThread ]
27
28
private static void Main ( string [ ] args )
28
29
{
29
30
StorageFolder storageFolder = ApplicationData . Current . LocalFolder ;
30
- NLog . LogManager . Configuration = new NLog . Config . XmlLoggingConfiguration ( Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "NLog.config" ) ) ;
31
- NLog . LogManager . Configuration . Variables [ "LogPath" ] = storageFolder . Path ;
31
+ LogManager . Configuration = new NLog . Config . XmlLoggingConfiguration ( Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "NLog.config" ) ) ;
32
+ LogManager . Configuration . Variables [ "LogPath" ] = storageFolder . Path ;
32
33
33
34
AppDomain . CurrentDomain . UnhandledException += UnhandledExceptionTrapper ;
34
35
@@ -41,12 +42,15 @@ private static void Main(string[] args)
41
42
// Only one instance of the fulltrust process allowed
42
43
// This happens if multiple instances of the UWP app are launched
43
44
using var mutex = new Mutex ( true , "FilesUwpFullTrust" , out bool isNew ) ;
44
- if ( ! isNew ) return ;
45
+ if ( ! isNew )
46
+ {
47
+ return ;
48
+ }
45
49
46
50
try
47
51
{
48
52
// Create shell COM object and get recycle bin folder
49
- recycler = new ShellFolder ( KNOWNFOLDERID . FOLDERID_RecycleBinFolder ) ;
53
+ recycler = new ShellFolder ( Shell32 . KNOWNFOLDERID . FOLDERID_RecycleBinFolder ) ;
50
54
ApplicationData . Current . LocalSettings . Values [ "RecycleBin_Title" ] = recycler . Name ;
51
55
52
56
// Create filesystem watcher to monitor recycle bin folder(s)
@@ -98,7 +102,7 @@ private static void UnhandledExceptionTrapper(object sender, UnhandledExceptionE
98
102
99
103
private static async void Watcher_Changed ( object sender , FileSystemEventArgs e )
100
104
{
101
- Debug . WriteLine ( "Reycle bin event: {0}, {1}" , e . ChangeType , e . FullPath ) ;
105
+ Debug . WriteLine ( $ "Reycle bin event: { e . ChangeType } , { e . FullPath } " ) ;
102
106
if ( connection != null )
103
107
{
104
108
// Send message to UWP app to refresh items
@@ -262,15 +266,15 @@ private static async Task parseRecycleBinAction(AppServiceRequestReceivedEventAr
262
266
{
263
267
case "Empty" :
264
268
// Shell function to empty recyclebin
265
- SHEmptyRecycleBin ( IntPtr . Zero , null , SHERB . SHERB_NOCONFIRMATION | SHERB . SHERB_NOPROGRESSUI ) ;
269
+ Shell32 . SHEmptyRecycleBin ( IntPtr . Zero , null , Shell32 . SHERB . SHERB_NOCONFIRMATION | Shell32 . SHERB . SHERB_NOPROGRESSUI ) ;
266
270
break ;
267
271
268
272
case "Query" :
269
273
var responseQuery = new ValueSet ( ) ;
270
- SHQUERYRBINFO queryBinInfo = new SHQUERYRBINFO ( ) ;
274
+ Shell32 . SHQUERYRBINFO queryBinInfo = new Shell32 . SHQUERYRBINFO ( ) ;
271
275
queryBinInfo . cbSize = ( uint ) Marshal . SizeOf ( queryBinInfo ) ;
272
- var res = SHQueryRecycleBin ( null , ref queryBinInfo ) ;
273
- if ( res == Vanara . PInvoke . HRESULT . S_OK )
276
+ var res = Shell32 . SHQueryRecycleBin ( null , ref queryBinInfo ) ;
277
+ if ( res == HRESULT . S_OK )
274
278
{
275
279
var numItems = queryBinInfo . i64NumItems ;
276
280
var binSize = queryBinInfo . i64Size ;
@@ -298,13 +302,13 @@ private static async Task parseRecycleBinAction(AppServiceRequestReceivedEventAr
298
302
continue ;
299
303
}
300
304
folderItem . Properties . TryGetValue < System . Runtime . InteropServices . ComTypes . FILETIME ? > (
301
- Vanara . PInvoke . Ole32 . PROPERTYKEY . System . DateCreated , out var fileTime ) ;
305
+ Ole32 . PROPERTYKEY . System . DateCreated , out var fileTime ) ;
302
306
var recycleDate = fileTime ? . ToDateTime ( ) . ToLocalTime ( ) ?? DateTime . Now ; // This is LocalTime
303
307
string fileSize = folderItem . Properties . TryGetValue < ulong ? > (
304
- Vanara . PInvoke . Ole32 . PROPERTYKEY . System . Size , out var fileSizeBytes ) ?
305
- folderItem . Properties . GetPropertyString ( Vanara . PInvoke . Ole32 . PROPERTYKEY . System . Size ) : null ;
308
+ Ole32 . PROPERTYKEY . System . Size , out var fileSizeBytes ) ?
309
+ folderItem . Properties . GetPropertyString ( Ole32 . PROPERTYKEY . System . Size ) : null ;
306
310
folderItem . Properties . TryGetValue < string > (
307
- Vanara . PInvoke . Ole32 . PROPERTYKEY . System . ItemTypeText , out var fileType ) ;
311
+ Ole32 . PROPERTYKEY . System . ItemTypeText , out var fileType ) ;
308
312
folderContentsList . Add ( new ShellFileItem ( isFolder , recyclePath , fileName , filePath , recycleDate , fileSize , fileSizeBytes ?? 0 , fileType ) ) ;
309
313
}
310
314
catch ( FileNotFoundException )
@@ -398,32 +402,42 @@ await Win32API.StartSTATask(() =>
398
402
}
399
403
else
400
404
{
401
- var groups = split . GroupBy ( x => new {
405
+ var groups = split . GroupBy ( x => new
406
+ {
402
407
Dir = Path . GetDirectoryName ( x ) ,
403
408
Prog = Win32API . GetFileAssociation ( x ) . Result ?? Path . GetExtension ( x )
404
409
} ) ;
405
410
foreach ( var group in groups )
406
411
{
407
- if ( ! group . Any ( ) ) continue ;
412
+ if ( ! group . Any ( ) )
413
+ {
414
+ continue ;
415
+ }
416
+
408
417
var files = group . Select ( x => new ShellItem ( x ) ) ;
409
418
using var sf = files . First ( ) . Parent ;
410
- Vanara . PInvoke . Shell32 . IContextMenu menu = null ;
419
+ Shell32 . IContextMenu menu = null ;
411
420
try
412
421
{
413
- menu = sf . GetChildrenUIObjects < Vanara . PInvoke . Shell32 . IContextMenu > ( null , files . ToArray ( ) ) ;
414
- menu . QueryContextMenu ( Vanara . PInvoke . HMENU . NULL , 0 , 0 , 0 , Vanara . PInvoke . Shell32 . CMF . CMF_DEFAULTONLY ) ;
415
- var pici = new Vanara . PInvoke . Shell32 . CMINVOKECOMMANDINFOEX ( ) ;
416
- pici . lpVerb = Vanara . PInvoke . Shell32 . CMDSTR_OPEN ;
417
- pici . nShow = Vanara . PInvoke . ShowWindowCommand . SW_SHOW ;
422
+ menu = sf . GetChildrenUIObjects < Shell32 . IContextMenu > ( null , files . ToArray ( ) ) ;
423
+ menu . QueryContextMenu ( HMENU . NULL , 0 , 0 , 0 , Shell32 . CMF . CMF_DEFAULTONLY ) ;
424
+ var pici = new Shell32 . CMINVOKECOMMANDINFOEX ( ) ;
425
+ pici . lpVerb = Shell32 . CMDSTR_OPEN ;
426
+ pici . nShow = ShowWindowCommand . SW_SHOW ;
418
427
pici . cbSize = ( uint ) Marshal . SizeOf ( pici ) ;
419
428
menu . InvokeCommand ( pici ) ;
420
429
}
421
430
finally
422
431
{
423
432
foreach ( var elem in files )
433
+ {
424
434
elem . Dispose ( ) ;
435
+ }
436
+
425
437
if ( menu != null )
438
+ {
426
439
Marshal . ReleaseComObject ( menu ) ;
440
+ }
427
441
}
428
442
}
429
443
}
@@ -483,7 +497,7 @@ private static string GetMtpPath(string executable)
483
497
{
484
498
if ( executable . StartsWith ( "\\ \\ ?\\ " ) )
485
499
{
486
- using var computer = new ShellFolder ( Vanara . PInvoke . Shell32 . KNOWNFOLDERID . FOLDERID_ComputerFolder ) ;
500
+ using var computer = new ShellFolder ( Shell32 . KNOWNFOLDERID . FOLDERID_ComputerFolder ) ;
487
501
using var device = computer . FirstOrDefault ( i => executable . Replace ( "\\ \\ ?\\ " , "" ) . StartsWith ( i . Name ) ) ;
488
502
var deviceId = device ? . ParsingName ;
489
503
var itemPath = Regex . Replace ( executable , @"^\\\\\?\\[^\\]*\\?" , "" ) ;
0 commit comments