@@ -5,22 +5,28 @@ namespace Files.App.Helpers
5
5
{
6
6
public static class FileExtensionHelpers
7
7
{
8
+ /// <summary>
9
+ /// Check if the file extension matches one of the specified extensions.
10
+ /// </summary>
11
+ /// <param name="filePathToCheck">Path or name or extension of the file to check.</param>
12
+ /// <param name="extensions">List of the extensions to check.</param>
13
+ /// <returns><c>true</c> if the filePathToCheck has one of the specified extensions;
14
+ /// otherwise, <c>false</c>.</returns>
15
+ public static bool HasExtension ( string ? filePathToCheck , params string [ ] extensions )
16
+ {
17
+ if ( string . IsNullOrWhiteSpace ( filePathToCheck ) )
18
+ return false ;
19
+ return extensions . Any ( ext => filePathToCheck . EndsWith ( ext , StringComparison . OrdinalIgnoreCase ) ) ;
20
+ }
21
+
8
22
/// <summary>
9
23
/// Check if the file extension is an image file.
10
24
/// </summary>
11
25
/// <param name="fileExtensionToCheck">The file extension to check.</param>
12
26
/// <returns><c>true</c> if the fileExtensionToCheck is an image;
13
27
/// otherwise, <c>false</c>.</returns>
14
28
public static bool IsImageFile ( string ? fileExtensionToCheck )
15
- {
16
- if ( string . IsNullOrWhiteSpace ( fileExtensionToCheck ) )
17
- return false ;
18
-
19
- return fileExtensionToCheck . Equals ( ".png" , StringComparison . OrdinalIgnoreCase ) ||
20
- fileExtensionToCheck . Equals ( ".jpg" , StringComparison . OrdinalIgnoreCase ) ||
21
- fileExtensionToCheck . Equals ( ".bmp" , StringComparison . OrdinalIgnoreCase ) ||
22
- fileExtensionToCheck . Equals ( ".jpeg" , StringComparison . OrdinalIgnoreCase ) ;
23
- }
29
+ => HasExtension ( fileExtensionToCheck , ".png" , ".bmp" , ".jpg" , ".jpeg" ) ;
24
30
25
31
/// <summary>
26
32
/// Check if the file extension is a PowerShell script.
@@ -29,10 +35,7 @@ public static bool IsImageFile(string? fileExtensionToCheck)
29
35
/// <returns><c>true</c> if the fileExtensionToCheck is a PowerShell script;
30
36
/// otherwise, <c>false</c>.</returns>
31
37
public static bool IsPowerShellFile ( string fileExtensionToCheck )
32
- {
33
- return ! string . IsNullOrEmpty ( fileExtensionToCheck ) &&
34
- fileExtensionToCheck . Equals ( ".ps1" , StringComparison . OrdinalIgnoreCase ) ;
35
- }
38
+ => HasExtension ( fileExtensionToCheck , ".ps1" ) ;
36
39
37
40
/// <summary>
38
41
/// Check if the file extension is a zip file.
@@ -41,13 +44,7 @@ public static bool IsPowerShellFile(string fileExtensionToCheck)
41
44
/// <returns><c>true</c> if the fileExtensionToCheck is a zip bundle file;
42
45
/// otherwise <c>false</c>.</returns>
43
46
public static bool IsZipFile ( string ? fileExtensionToCheck )
44
- {
45
- if ( string . IsNullOrWhiteSpace ( fileExtensionToCheck ) )
46
- return false ;
47
-
48
- return new [ ] { ".zip" , ".msix" , ".appx" , ".msixbundle" , ".7z" , ".rar" , ".tar" }
49
- . Contains ( fileExtensionToCheck , StringComparer . OrdinalIgnoreCase ) ;
50
- }
47
+ => HasExtension ( fileExtensionToCheck , ".zip" , ".msix" , ".appx" , ".msixbundle" , ".7z" , ".rar" , ".tar" ) ;
51
48
52
49
public static bool IsBrowsableZipFile ( string ? filePath , out string ? ext )
53
50
{
@@ -62,11 +59,14 @@ public static bool IsBrowsableZipFile(string? filePath, out string? ext)
62
59
return ext is not null ;
63
60
}
64
61
62
+ /// <summary>
63
+ /// Check if the file extension is a driver inf file.
64
+ /// </summary>
65
+ /// <param name="fileExtensionToCheck">The file extension to check.</param>
66
+ /// <returns><c>true</c> if the fileExtensionToCheck is an inf file;
67
+ /// otherwise <c>false</c>.</returns>
65
68
public static bool IsInfFile ( string ? fileExtensionToCheck )
66
- {
67
- return ! string . IsNullOrWhiteSpace ( fileExtensionToCheck ) &&
68
- fileExtensionToCheck . Equals ( ".inf" , StringComparison . OrdinalIgnoreCase ) ;
69
- }
69
+ => HasExtension ( fileExtensionToCheck , ".inf" ) ;
70
70
71
71
/// <summary>
72
72
/// Check if the file extension is a font file.
@@ -76,15 +76,7 @@ public static bool IsInfFile(string? fileExtensionToCheck)
76
76
/// otherwise <c>false</c>.</returns>
77
77
/// <remarks>Font file types are; fon, otf, ttc, ttf</remarks>
78
78
public static bool IsFontFile ( string ? fileExtensionToCheck )
79
- {
80
- if ( string . IsNullOrWhiteSpace ( fileExtensionToCheck ) )
81
- return false ;
82
-
83
- return fileExtensionToCheck . Equals ( ".fon" , StringComparison . OrdinalIgnoreCase ) ||
84
- fileExtensionToCheck . Equals ( ".otf" , StringComparison . OrdinalIgnoreCase ) ||
85
- fileExtensionToCheck . Equals ( ".ttc" , StringComparison . OrdinalIgnoreCase ) ||
86
- fileExtensionToCheck . Equals ( ".ttf" , StringComparison . OrdinalIgnoreCase ) ;
87
- }
79
+ => HasExtension ( fileExtensionToCheck , ".fon" , ".otf" , ".ttc" , ".ttf" ) ;
88
80
89
81
/// <summary>
90
82
/// Check if the file path is a shortcut file.
@@ -94,18 +86,50 @@ public static bool IsFontFile(string? fileExtensionToCheck)
94
86
/// otherwise <c>false</c>.</returns>
95
87
/// <remarks>Shortcut file type is .lnk</remarks>
96
88
public static bool IsShortcutFile ( string ? filePathToCheck )
97
- {
98
- return ! string . IsNullOrWhiteSpace ( filePathToCheck ) &&
99
- filePathToCheck . EndsWith ( ".lnk" , StringComparison . OrdinalIgnoreCase ) ;
100
- }
89
+ => HasExtension ( filePathToCheck , ".lnk" ) ;
101
90
91
+ /// <summary>
92
+ /// Check if the file path is a web link file.
93
+ /// </summary>
94
+ /// <param name="filePathToCheck">The file path to check.</param>
95
+ /// <returns><c>true</c> if the filePathToCheck is a web link file;
96
+ /// otherwise <c>false</c>.</returns>
97
+ /// <remarks>Web link file type is .url</remarks>
102
98
public static bool IsWebLinkFile ( string ? filePathToCheck )
103
- {
104
- return ! string . IsNullOrWhiteSpace ( filePathToCheck ) &&
105
- filePathToCheck . EndsWith ( ".url" , StringComparison . OrdinalIgnoreCase ) ;
106
- }
99
+ => HasExtension ( filePathToCheck , ".url" ) ;
107
100
108
101
public static bool IsShortcutOrUrlFile ( string ? filePathToCheck )
109
- => IsShortcutFile ( filePathToCheck ) || IsWebLinkFile ( filePathToCheck ) ;
102
+ => HasExtension ( filePathToCheck , ".lnk" , ".url" ) ;
103
+
104
+ /// <summary>
105
+ /// Check if the file path is an executable file.
106
+ /// </summary>
107
+ /// <param name="filePathToCheck">The file path to check.</param>
108
+ /// <returns><c>true</c> if the filePathToCheck is an executable file;
109
+ /// otherwise <c>false</c>.</returns>
110
+ /// /// <remarks>Executable file types are; exe, bat, cmd</remarks>
111
+ public static bool IsExecutableFile ( string ? filePathToCheck , bool exeOnly = false )
112
+ => exeOnly ?
113
+ HasExtension ( filePathToCheck , ".exe" ) :
114
+ HasExtension ( filePathToCheck , ".exe" , ".bat" , ".cmd" ) ;
115
+
116
+ /// <summary>
117
+ /// Check if the file path is an msi installer file.
118
+ /// </summary>
119
+ /// <param name="filePathToCheck">The file path to check.</param>
120
+ /// <returns><c>true</c> if the filePathToCheck is an msi installer file;
121
+ /// otherwise <c>false</c>.</returns>
122
+ public static bool IsMsiFile ( string ? filePathToCheck )
123
+ => HasExtension ( filePathToCheck , ".msi" ) ;
124
+
125
+ /// <summary>
126
+ /// Check if the file extension is a vhd disk file.
127
+ /// </summary>
128
+ /// <param name="fileExtensionToCheck">The file extension to check.</param>
129
+ /// <returns><c>true</c> if the fileExtensionToCheck is a vhd disk file;
130
+ /// otherwise <c>false</c>.</returns>
131
+ /// <remarks>Vhd disk file types are; vhd, vhdx</remarks>
132
+ public static bool IsVhdFile ( string ? fileExtensionToCheck )
133
+ => HasExtension ( fileExtensionToCheck , ".vhd" , ".vhdx" ) ;
110
134
}
111
135
}
0 commit comments