|
| 1 | +using System.Collections.Frozen; |
| 2 | + |
| 3 | +namespace DevWinUI; |
| 4 | + |
| 5 | +public static class FileExtensionHelpers |
| 6 | +{ |
| 7 | + private static readonly FrozenSet<string> _signableTypes = new HashSet<string>() |
| 8 | + { |
| 9 | + ".aab", ".apk", ".application", ".appx", ".appxbundle", ".arx", ".cab", ".cat", ".cbx", |
| 10 | + ".cpl", ".crx", ".dbx", ".deploy", ".dll", ".doc", ".docm", ".dot", ".dotm", ".drx", |
| 11 | + ".ear", ".efi", ".exe", ".jar", ".js", ".manifest", ".mpp", ".mpt", ".msi", ".msix", |
| 12 | + ".msixbundle", ".msm", ".msp", ".nupkg", ".ocx", ".pot", ".potm", ".ppa", ".ppam", ".pps", |
| 13 | + ".ppsm", ".ppt", ".pptm", ".ps1", ".psm1", ".psi", ".pub", ".sar", ".stl", ".sys", ".vbs", |
| 14 | + ".vdw", ".vdx", ".vsd", ".vsdm", ".vss", ".vssm", ".vst", ".vstm", ".vsto", ".vsix", ".vsx", ".vtx", |
| 15 | + ".vxd", ".war", ".wiz", ".wsf", ".xap", ".xla", ".xlam", ".xls", ".xlsb", ".xlsm", ".xlt", |
| 16 | + ".xltm", ".xlsm", ".xsn" |
| 17 | + }.ToFrozenSet(); |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Check if the file extension matches one of the specified extensions. |
| 21 | + /// </summary> |
| 22 | + /// <param name="filePathToCheck">Path or name or extension of the file to check.</param> |
| 23 | + /// <param name="extensions">List of the extensions to check.</param> |
| 24 | + /// <returns><c>true</c> if the filePathToCheck has one of the specified extensions; otherwise, <c>false</c>.</returns> |
| 25 | + public static bool HasExtension(string? filePathToCheck, params string[] extensions) |
| 26 | + { |
| 27 | + if (string.IsNullOrWhiteSpace(filePathToCheck)) |
| 28 | + return false; |
| 29 | + |
| 30 | + if (Directory.Exists(filePathToCheck)) |
| 31 | + return false; |
| 32 | + |
| 33 | + return extensions.Any(ext => Path.GetExtension(filePathToCheck).Equals(ext, StringComparison.OrdinalIgnoreCase)); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Check if the file extension is an image file. |
| 38 | + /// </summary> |
| 39 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 40 | + /// <returns><c>true</c> if the fileExtensionToCheck is an image; otherwise, <c>false</c>.</returns> |
| 41 | + public static bool IsImageFile(string? fileExtensionToCheck) |
| 42 | + { |
| 43 | + return HasExtension(fileExtensionToCheck, ".png", ".bmp", ".jpg", ".jpeg", ".jfif", ".gif", ".tiff", ".tif", ".webp", ".jxr"); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Checks if the file can be set as wallpaper. |
| 48 | + /// </summary> |
| 49 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 50 | + /// <returns><c>true</c> if the fileExtensionToCheck is an image; otherwise, <c>false</c>.</returns> |
| 51 | + public static bool IsCompatibleToSetAsWindowsWallpaper(string? fileExtensionToCheck) |
| 52 | + { |
| 53 | + return HasExtension(fileExtensionToCheck, ".png", ".bmp", ".jpg", ".jpeg", ".jfif", ".gif", ".tiff", ".tif", ".jxr"); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Check if the file extension is an audio file. |
| 58 | + /// </summary> |
| 59 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 60 | + /// <returns><c>true</c> if the fileExtensionToCheck is an audio file; otherwise, <c>false</c>.</returns> |
| 61 | + public static bool IsAudioFile(string? fileExtensionToCheck) |
| 62 | + { |
| 63 | + return HasExtension(fileExtensionToCheck, ".mp3", ".m4a", ".wav", ".wma", ".aac", ".adt", ".adts", ".cda", ".flac"); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Check if the file extension is a video file. |
| 68 | + /// </summary> |
| 69 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 70 | + /// <returns><c>true</c> if the fileExtensionToCheck is a video file; otherwise, <c>false</c>.</returns> |
| 71 | + public static bool IsVideoFile(string? fileExtensionToCheck) |
| 72 | + { |
| 73 | + return HasExtension(fileExtensionToCheck, ".mp4", ".webm", ".ogg", ".mov", ".qt", ".m4v", ".mp4v", ".3g2", ".3gp2", ".3gp", ".3gpp", ".mkv"); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Check if the file extension is a PowerShell script. |
| 78 | + /// </summary> |
| 79 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 80 | + /// <returns><c>true</c> if the fileExtensionToCheck is a PowerShell script; otherwise, <c>false</c>.</returns> |
| 81 | + public static bool IsPowerShellFile(string fileExtensionToCheck) |
| 82 | + { |
| 83 | + return HasExtension(fileExtensionToCheck, ".ps1"); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Check if the file extension is a Batch file. |
| 88 | + /// </summary> |
| 89 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 90 | + /// <returns><c>true</c> if the fileExtensionToCheck is a Batch file; otherwise, <c>false</c>.</returns> |
| 91 | + public static bool IsBatchFile(string fileExtensionToCheck) |
| 92 | + { |
| 93 | + return HasExtension(fileExtensionToCheck, ".bat"); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Check if the file extension is a zip file. |
| 98 | + /// </summary> |
| 99 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 100 | + /// <returns><c>true</c> if the fileExtensionToCheck is a zip bundle file; otherwise, <c>false</c>.</returns> |
| 101 | + public static bool IsZipFile(string? fileExtensionToCheck) |
| 102 | + { |
| 103 | + return HasExtension(fileExtensionToCheck, ".zip", ".msix", ".appx", ".msixbundle", ".appxbundle", ".7z", ".rar", ".tar", ".mcpack", ".mcworld", ".mrpack", ".jar", ".gz", ".lzh"); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Check if the file extension is a driver inf file. |
| 108 | + /// </summary> |
| 109 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 110 | + /// <returns><c>true</c> if the fileExtensionToCheck is an inf file; otherwise <c>false</c>.</returns> |
| 111 | + public static bool IsInfFile(string? fileExtensionToCheck) |
| 112 | + { |
| 113 | + return HasExtension(fileExtensionToCheck, ".inf"); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Check if the file extension is a font file. |
| 118 | + /// </summary> |
| 119 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 120 | + /// <returns><c>true</c> if the fileExtensionToCheck is a font file; otherwise <c>false</c>.</returns> |
| 121 | + /// <remarks>Font file types are; fon, otf, ttc, ttf</remarks> |
| 122 | + public static bool IsFontFile(string? fileExtensionToCheck) |
| 123 | + { |
| 124 | + return HasExtension(fileExtensionToCheck, ".fon", ".otf", ".ttc", ".ttf"); |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Check if the file path is a shortcut file. |
| 129 | + /// </summary> |
| 130 | + /// <param name="filePathToCheck">The file path to check.</param> |
| 131 | + /// <returns><c>true</c> if the filePathToCheck is a shortcut file; otherwise, <c>false</c>.</returns> |
| 132 | + /// <remarks>Shortcut file type is .lnk</remarks> |
| 133 | + public static bool IsShortcutFile(string? filePathToCheck) |
| 134 | + { |
| 135 | + return HasExtension(filePathToCheck, ".lnk"); |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Check if the file path is a web link file. |
| 140 | + /// </summary> |
| 141 | + /// <param name="filePathToCheck">The file path to check.</param> |
| 142 | + /// <returns><c>true</c> if the filePathToCheck is a web link file; otherwise, <c>false</c>.</returns> |
| 143 | + /// <remarks>Web link file type is .url</remarks> |
| 144 | + public static bool IsWebLinkFile(string? filePathToCheck) |
| 145 | + { |
| 146 | + return HasExtension(filePathToCheck, ".url"); |
| 147 | + } |
| 148 | + |
| 149 | + public static bool IsShortcutOrUrlFile(string? filePathToCheck) |
| 150 | + { |
| 151 | + return HasExtension(filePathToCheck, ".lnk", ".url"); |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Check if the file path is an executable file. |
| 156 | + /// </summary> |
| 157 | + /// <param name="filePathToCheck">The file path to check.</param> |
| 158 | + /// <returns><c>true</c> if the filePathToCheck is an executable file; otherwise, <c>false</c>.</returns> |
| 159 | + /// /// <remarks>Executable file types are; exe, bat, cmd</remarks> |
| 160 | + public static bool IsExecutableFile(string? filePathToCheck, bool exeOnly = false) |
| 161 | + { |
| 162 | + return |
| 163 | + exeOnly |
| 164 | + ? HasExtension(filePathToCheck, ".exe") |
| 165 | + : HasExtension(filePathToCheck, ".exe", ".bat", ".cmd", ".ahk"); |
| 166 | + } |
| 167 | + |
| 168 | + /// <summary> |
| 169 | + /// Check if the file path is an Auto Hot Key file. |
| 170 | + /// </summary> |
| 171 | + /// <param name="filePathToCheck">The file path to check.</param> |
| 172 | + /// <returns><c>true</c> if the filePathToCheck is an Auto Hot Key file; otherwise, <c>false</c>.</returns> |
| 173 | + public static bool IsAhkFile(string? filePathToCheck) |
| 174 | + { |
| 175 | + return HasExtension(filePathToCheck, ".ahk"); |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// Check if the file path is a cmd file. |
| 180 | + /// </summary> |
| 181 | + /// <param name="filePathToCheck">The file path to check.</param> |
| 182 | + /// <returns><c>true</c> if the filePathToCheck is a cmd file; otherwise, <c>false</c>.</returns> |
| 183 | + public static bool IsCmdFile(string? filePathToCheck) |
| 184 | + { |
| 185 | + return HasExtension(filePathToCheck, ".cmd"); |
| 186 | + } |
| 187 | + |
| 188 | + /// <summary> |
| 189 | + /// Check if the file path is an msi installer file. |
| 190 | + /// </summary> |
| 191 | + /// <param name="filePathToCheck">The file path to check.</param> |
| 192 | + /// <returns><c>true</c> if the filePathToCheck is an msi installer file; otherwise, <c>false</c>.</returns> |
| 193 | + public static bool IsMsiFile(string? filePathToCheck) |
| 194 | + { |
| 195 | + return HasExtension(filePathToCheck, ".msi"); |
| 196 | + } |
| 197 | + |
| 198 | + /// <summary> |
| 199 | + /// Check if the file extension is a vhd disk file. |
| 200 | + /// </summary> |
| 201 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 202 | + /// <returns><c>true</c> if the fileExtensionToCheck is a vhd disk file; otherwise, <c>false</c>.</returns> |
| 203 | + /// <remarks>Vhd disk file types are; vhd, vhdx</remarks> |
| 204 | + public static bool IsVhdFile(string? fileExtensionToCheck) |
| 205 | + { |
| 206 | + return HasExtension(fileExtensionToCheck, ".vhd", ".vhdx"); |
| 207 | + } |
| 208 | + |
| 209 | + /// <summary> |
| 210 | + /// Check if the file extension is a screen saver file. |
| 211 | + /// </summary> |
| 212 | + /// <param name="fileExtensionToCheck">The file extension to check.</param> |
| 213 | + /// <returns><c>true</c> if the fileExtensionToCheck is a screen saver file; otherwise, <c>false</c>.</returns> |
| 214 | + /// <remarks>Screen saver file types are; scr</remarks> |
| 215 | + public static bool IsScreenSaverFile(string? fileExtensionToCheck) |
| 216 | + { |
| 217 | + return HasExtension(fileExtensionToCheck, ".scr"); |
| 218 | + } |
| 219 | + |
| 220 | + /// <summary> |
| 221 | + /// Check if the file extension is a media (audio/video) file. |
| 222 | + /// </summary> |
| 223 | + /// <param name="filePathToCheck">The file extension to check.</param> |
| 224 | + /// <returns><c>true</c> if the filePathToCheck is a media file; otherwise, <c>false</c>.</returns> |
| 225 | + public static bool IsMediaFile(string? filePathToCheck) |
| 226 | + { |
| 227 | + return HasExtension( |
| 228 | + filePathToCheck, ".mp4", ".m4v", ".mp4v", ".3g2", ".3gp2", ".3gp", ".3gpp", |
| 229 | + ".mpg", ".mp2", ".mpeg", ".mpe", ".mpv", ".mkv", ".ogg", ".avi", ".wmv", ".mov", ".qt"); |
| 230 | + } |
| 231 | + |
| 232 | + /// <summary> |
| 233 | + /// Check if the file extension is a certificate file. |
| 234 | + /// </summary> |
| 235 | + /// <param name="filePathToCheck"></param> |
| 236 | + /// <returns><c>true</c> if the filePathToCheck is a certificate file; otherwise, <c>false</c>.</returns> |
| 237 | + public static bool IsCertificateFile(string? filePathToCheck) |
| 238 | + { |
| 239 | + return HasExtension(filePathToCheck, ".cer", ".crt", ".der", ".pfx"); |
| 240 | + } |
| 241 | + |
| 242 | + /// <summary> |
| 243 | + /// Check if the file extension is a Script file. |
| 244 | + /// </summary> |
| 245 | + /// <param name="filePathToCheck"></param> |
| 246 | + /// <returns><c>true</c> if the filePathToCheck is a script file; otherwise, <c>false</c>.</returns> |
| 247 | + public static bool IsScriptFile(string? filePathToCheck) |
| 248 | + { |
| 249 | + return HasExtension(filePathToCheck, ".py", ".ahk", ".bat", ".cmd", ".ps1"); |
| 250 | + } |
| 251 | + |
| 252 | + /// <summary> |
| 253 | + /// Check if the file extension is a system file. |
| 254 | + /// </summary> |
| 255 | + /// <param name="filePathToCheck"></param> |
| 256 | + /// <returns><c>true</c> if the filePathToCheck is a system file; otherwise, <c>false</c>.</returns> |
| 257 | + public static bool IsSystemFile(string? filePathToCheck) |
| 258 | + { |
| 259 | + return HasExtension(filePathToCheck, ".dll", ".exe", ".sys", ".inf"); |
| 260 | + } |
| 261 | + |
| 262 | + /// <summary> |
| 263 | + /// Check if the file is signable. |
| 264 | + /// </summary> |
| 265 | + /// <param name="filePathToCheck"></param> |
| 266 | + /// <returns><c>true</c> if the filePathToCheck is a signable file; otherwise, <c>false</c>.</returns> |
| 267 | + public static bool IsSignableFile(string? filePathToCheck, bool isExtension = false) |
| 268 | + { |
| 269 | + if (string.IsNullOrWhiteSpace(filePathToCheck)) |
| 270 | + return false; |
| 271 | + |
| 272 | + if (!isExtension) |
| 273 | + filePathToCheck = Path.GetExtension(filePathToCheck); |
| 274 | + |
| 275 | + return _signableTypes.Contains(filePathToCheck); |
| 276 | + } |
| 277 | +} |
0 commit comments