1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 04:05:40 +00:00

Don't make exception throwing optional

This commit is contained in:
Oliver Booth 2022-11-29 17:17:13 +00:00
parent 7a1ecc6a01
commit 00d784eef0
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 8 additions and 55 deletions

View File

@ -11,7 +11,7 @@
- X10D: Added `Color.Deconstruct()` - with optional alpha parameter - X10D: Added `Color.Deconstruct()` - with optional alpha parameter
- X10D: Added `Color.GetClosestConsoleColor()` - X10D: Added `Color.GetClosestConsoleColor()`
- X10D: Added `DateTime.GetIso8601WeekOfYear()` and `DateTimeOffset.GetIso8601WeekOfYear()` - X10D: Added `DateTime.GetIso8601WeekOfYear()` and `DateTimeOffset.GetIso8601WeekOfYear()`
- X10D: Added `DirectoryInfo.Clear([bool])` - X10D: Added `DirectoryInfo.Clear()`
- X10D: Added `IEnumerable<T>.CountWhereNot(Func<T, bool>)` - X10D: Added `IEnumerable<T>.CountWhereNot(Func<T, bool>)`
- X10D: Added `IEnumerable<T>.FirstWhereNot(Func<T, bool>)` - X10D: Added `IEnumerable<T>.FirstWhereNot(Func<T, bool>)`
- X10D: Added `IEnumerable<T>.FirstWhereNotOrDefault(Func<T, bool>)` - X10D: Added `IEnumerable<T>.FirstWhereNotOrDefault(Func<T, bool>)`

View File

@ -11,22 +11,8 @@ public static class DirectoryInfoExtensions
/// Removes all files and subdirectories in this directory, recursively, without deleting this directory. /// Removes all files and subdirectories in this directory, recursively, without deleting this directory.
/// </summary> /// </summary>
/// <param name="directory">The directory to clear.</param> /// <param name="directory">The directory to clear.</param>
public static void Clear(this DirectoryInfo directory)
{
directory.Clear(false);
}
/// <summary>
/// Removes all files and subdirectories in this directory, recursively, without deleting this directory.
/// </summary>
/// <param name="directory">The directory to clear.</param>
/// <param name="throwOnError">
/// <see langword="true" /> to throw any exceptions which were caught during the operation; otherwise,
/// <see langword="false." />
/// </param>
/// <exception cref="DirectoryNotFoundException"> /// <exception cref="DirectoryNotFoundException">
/// The directory described by this <see cref="DirectoryInfo" /> object does not exist or could not be found. This /// The directory described by this <see cref="DirectoryInfo" /> object does not exist or could not be found.
/// exception is not thrown if <paramref name="throwOnError" /> is <see langword="false" />.
/// </exception> /// </exception>
/// <exception cref="IOException"> /// <exception cref="IOException">
/// A target file is open or memory-mapped on a computer running Microsoft Windows NT. /// A target file is open or memory-mapped on a computer running Microsoft Windows NT.
@ -42,16 +28,10 @@ public static class DirectoryInfoExtensions
/// -or- /// -or-
/// There is an open handle on the directory or on one of its files, and the operating system is Windows XP or earlier. /// There is an open handle on the directory or on one of its files, and the operating system is Windows XP or earlier.
/// This open handle can result from enumerating directories and files. /// This open handle can result from enumerating directories and files.
/// This exception is not thrown if <paramref name="throwOnError" /> is <see langword="false" />.
/// </exception> /// </exception>
/// <exception cref="SecurityException"> /// <exception cref="SecurityException">The caller does not have the required permission.</exception>
/// The caller does not have the required permission. This exception is not thrown if <paramref name="throwOnError" /> is /// <exception cref="UnauthorizedAccessException">This directory or one of its children contain a read-only file.</exception>
/// <see langword="false" />. public static void Clear(this DirectoryInfo directory)
/// </exception>
/// <exception cref="UnauthorizedAccessException">This directory or one of its children contain a read-only file. This
/// exception is not thrown if <paramref name="throwOnError" /> is <see langword="false" />.
/// </exception>
public static void Clear(this DirectoryInfo directory, bool throwOnError)
{ {
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(directory); ArgumentNullException.ThrowIfNull(directory);
@ -64,44 +44,17 @@ public static class DirectoryInfoExtensions
if (!directory.Exists) if (!directory.Exists)
{ {
if (throwOnError) throw new DirectoryNotFoundException();
{
throw new DirectoryNotFoundException();
}
return;
} }
foreach (FileInfo file in directory.EnumerateFiles()) foreach (FileInfo file in directory.EnumerateFiles())
{ {
try file.Delete();
{
file.Delete();
}
catch when (throwOnError)
{
throw;
}
catch
{
// do nothing
}
} }
foreach (DirectoryInfo childDirectory in directory.EnumerateDirectories()) foreach (DirectoryInfo childDirectory in directory.EnumerateDirectories())
{ {
try childDirectory.Delete(true);
{
childDirectory.Delete(true);
}
catch when (throwOnError)
{
throw;
}
catch
{
// do nothing
}
} }
} }
} }