1
1
using Files . Enums ;
2
2
using Files . Filesystem ;
3
+ using System ;
4
+ using System . Diagnostics ;
3
5
using System . Threading . Tasks ;
4
6
using Windows . Storage ;
5
7
@@ -20,35 +22,83 @@ public static async Task<TOut> ToStorageItem<TOut>(string path, IShellPage assoc
20
22
FilesystemResult < StorageFile > file = null ;
21
23
FilesystemResult < StorageFolder > folder = null ;
22
24
23
- if ( associatedInstance == null )
25
+ if ( path . ToLower ( ) . EndsWith ( ".lnk" ) || path . ToLower ( ) . EndsWith ( ".url" ) )
24
26
{
25
- file = await FilesystemTasks . Wrap ( ( ) => StorageFileExtensions . DangerousGetFileFromPathAsync ( path ) ) ;
27
+ // TODO: In the future, when IStorageItemWithPath will inherit from IStorageItem,
28
+ // we could implement this code here for getting .lnk files
29
+ // for now, we can't
30
+
31
+ return default ( TOut ) ;
26
32
27
- if ( ! file )
33
+ if ( false ) // Prevent unnecessary exceptions
28
34
{
29
- folder = await FilesystemTasks . Wrap ( ( ) => StorageFileExtensions . DangerousGetFolderFromPathAsync ( path ) ) ;
35
+ Debugger . Break ( ) ;
36
+ throw new ArgumentException ( "Function ToStorageItem<TOut>() does not support converting from .lnk and .url files" ) ;
30
37
}
31
38
}
32
- else
33
- {
34
- file = await associatedInstance ? . FilesystemViewModel ? . GetFileFromPathAsync ( path ) ;
35
39
36
- if ( ! file )
40
+ if ( typeof ( IStorageFile ) . IsAssignableFrom ( typeof ( TOut ) ) )
41
+ {
42
+ await GetFile ( ) ;
43
+ }
44
+ else if ( typeof ( IStorageFolder ) . IsAssignableFrom ( typeof ( TOut ) ) )
45
+ {
46
+ await GetFolder ( ) ;
47
+ }
48
+ else if ( typeof ( IStorageItem ) . IsAssignableFrom ( typeof ( TOut ) ) )
49
+ {
50
+ if ( System . IO . Path . HasExtension ( path ) ) // Probably a file
37
51
{
38
- folder = await associatedInstance ? . FilesystemViewModel ? . GetFolderFromPathAsync ( path ) ;
52
+ await GetFile ( ) ;
53
+ }
54
+ else // Possibly a folder
55
+ {
56
+ await GetFolder ( ) ;
57
+
58
+ if ( ! folder )
59
+ {
60
+ // It wasn't a folder, so check file then because it wasn't checked
61
+ await GetFile ( ) ;
62
+ }
39
63
}
40
64
}
41
65
42
- if ( file )
66
+ if ( file != null && file )
43
67
{
44
68
return ( TOut ) ( IStorageItem ) file . Result ;
45
69
}
46
- else if ( folder )
70
+ else if ( folder != null && folder )
47
71
{
48
72
return ( TOut ) ( IStorageItem ) folder . Result ;
49
73
}
50
74
51
75
return default ( TOut ) ;
76
+
77
+ // Extensions
78
+
79
+ async Task GetFile ( )
80
+ {
81
+ if ( associatedInstance == null )
82
+ {
83
+ file = await FilesystemTasks . Wrap ( ( ) => StorageFileExtensions . DangerousGetFileFromPathAsync ( path ) ) ;
84
+ }
85
+ else
86
+ {
87
+ file = await associatedInstance ? . FilesystemViewModel ? . GetFileFromPathAsync ( path ) ;
88
+ }
89
+ }
90
+
91
+ async Task GetFolder ( )
92
+ {
93
+ if ( associatedInstance == null )
94
+ {
95
+ folder = await FilesystemTasks . Wrap ( ( ) => StorageFileExtensions . DangerousGetFolderFromPathAsync ( path ) ) ;
96
+ }
97
+ else
98
+ {
99
+ folder = await associatedInstance ? . FilesystemViewModel ? . GetFolderFromPathAsync ( path ) ;
100
+ }
101
+ }
52
102
}
53
103
54
104
public static async Task < FilesystemResult < IStorageItem > > ToStorageItemResult ( this IStorageItemWithPath item , IShellPage associatedInstance = null )
@@ -114,4 +164,4 @@ public static FilesystemResult<T> ToType<T, V>(FilesystemResult<V> result) where
114
164
return new FilesystemResult < T > ( result . Result as T , result . ErrorCode ) ;
115
165
}
116
166
}
117
- }
167
+ }
0 commit comments