1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:28:48 +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.GetClosestConsoleColor()`
- 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>.FirstWhereNot(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.
/// </summary>
/// <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">
/// The directory described by this <see cref="DirectoryInfo" /> object does not exist or could not be found. This
/// exception is not thrown if <paramref name="throwOnError" /> is <see langword="false" />.
/// The directory described by this <see cref="DirectoryInfo" /> object does not exist or could not be found.
/// </exception>
/// <exception cref="IOException">
/// A target file is open or memory-mapped on a computer running Microsoft Windows NT.
@ -42,16 +28,10 @@ public static class DirectoryInfoExtensions
/// -or-
/// 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 exception is not thrown if <paramref name="throwOnError" /> is <see langword="false" />.
/// </exception>
/// <exception cref="SecurityException">
/// The caller does not have the required permission. This exception is not thrown if <paramref name="throwOnError" /> is
/// <see langword="false" />.
/// </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)
/// <exception cref="SecurityException">The caller does not have the required permission.</exception>
/// <exception cref="UnauthorizedAccessException">This directory or one of its children contain a read-only file.</exception>
public static void Clear(this DirectoryInfo directory)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(directory);
@ -64,44 +44,17 @@ public static class DirectoryInfoExtensions
if (!directory.Exists)
{
if (throwOnError)
{
throw new DirectoryNotFoundException();
}
return;
throw new DirectoryNotFoundException();
}
foreach (FileInfo file in directory.EnumerateFiles())
{
try
{
file.Delete();
}
catch when (throwOnError)
{
throw;
}
catch
{
// do nothing
}
file.Delete();
}
foreach (DirectoryInfo childDirectory in directory.EnumerateDirectories())
{
try
{
childDirectory.Delete(true);
}
catch when (throwOnError)
{
throw;
}
catch
{
// do nothing
}
childDirectory.Delete(true);
}
}
}