Class FileRemover
- Namespace
- Switchboard
- Assembly
- Switchboard.FileUtilities.dll
Provides methods for removing excess files from a directory.
public static class FileRemover
- Inheritance
-
FileRemover
Methods
RemoveFilesBeyondLimit(int, string, string, string)
Removes excess files from a directory
.
All files that start with fileNamePrefix
and end with fileNameSuffix
are sorted by CreationTimeUtc.
If the fileNamePrefix
and fileNameSuffix
are both empty, all files in the directory
will be considered.
Then, the size of each file is added to a cumulative sum.
When adding a file's size causes the sum to exceed the byteLimit
,
that file and all remaining files will be removed from the directory
.
public static void RemoveFilesBeyondLimit(int byteLimit, string directory, string fileNamePrefix, string fileNameSuffix)
Parameters
byteLimit
intThe maximum total size in bytes allowed for all files that match the file name pattern.
directory
stringThe directory to search for excess files.
fileNamePrefix
stringThe start of the file name pattern, which may be empty.
fileNameSuffix
stringThe end of the file name pattern. For example, the file name extension, which may be empty.
Exceptions
- ArgumentNullException
Thrown if the
directory
,fileNamePrefix
, orfileNameSuffix
are null.- ArgumentException
Thrown if
directory
,fileNamePrefix
, orfileNameSuffix
are Empty. \ OR \ Thrown ifdirectory
contains invalid path characters. \ OR \ Thrown iffileNamePrefix
orfileNameSuffix
contain invalid file name characters, for the current platform. \ OR \ Thrown if the platform is Windows, and thefileNameSuffix
ends with a period or space character.- SecurityException
The caller does not have the required permission to access the directory or files.
- DirectoryNotFoundException
The specified path is invalid.
- UnauthorizedAccessException
The caller does not have the required permission to delete a file.
- PathTooLongException
The specified path exceeds the system-defined maximum length.
- IOException
A file to be deleted is in use.
- AggregateException
Thrown if multiple exceptions occur during the course of this method.
RemoveFilesBeyondLimit(int, string, string, string, bool)
Removes excess files from a directory
.
All files that start with fileNamePrefix
and end with fileNameSuffix
are sorted by CreationTimeUtc.
If the fileNamePrefix
and fileNameSuffix
are both empty, all files in the directory
will be considered.
Then, the size of each file is added to a cumulative sum.
When adding a file's size causes the sum to exceed the byteLimit
,
that file and all remaining files will be removed from the directory
.
public static void RemoveFilesBeyondLimit(int byteLimit, string directory, string fileNamePrefix, string fileNameSuffix, bool leaveOneFile)
Parameters
byteLimit
intThe maximum total size in bytes allowed for all files that match the file name pattern.
directory
stringThe directory to search for excess files.
fileNamePrefix
stringThe start of the file name pattern, which may be empty.
fileNameSuffix
stringThe end of the file name pattern. For example, the file name extension, which may be empty.
leaveOneFile
boolIf true, the first sorted file will not be removed, even if it exceeds the
byteLimit
.
Exceptions
- ArgumentNullException
Thrown if the
directory
,fileNamePrefix
, orfileNameSuffix
are null.- ArgumentException
Thrown if
directory
,fileNamePrefix
, orfileNameSuffix
are Empty. \ OR \ Thrown ifdirectory
contains invalid path characters. \ OR \ Thrown iffileNamePrefix
orfileNameSuffix
contain invalid file name characters, for the current platform. \ OR \ Thrown if the platform is Windows, and thefileNameSuffix
ends with a period or space character.- SecurityException
The caller does not have the required permission to access the directory or files.
- DirectoryNotFoundException
The specified path is invalid.
- UnauthorizedAccessException
The caller does not have the required permission to delete a file.
- PathTooLongException
The specified path exceeds the system-defined maximum length.
- IOException
A file to be deleted is in use.
- AggregateException
Thrown if multiple exceptions occur during the course of this method.
RemoveFilesBeyondLimit(int, string, string, string, bool, Comparison<FileSystemInfo>)
Removes excess files from a directory
.
All files that start with fileNamePrefix
and end with fileNameSuffix
are sorted according to the sortComparison
method.
If the fileNamePrefix
and fileNameSuffix
are both empty, all files in the directory
will be considered.
Then, the size of each file is added to a cumulative sum.
When adding a file's size causes the sum to exceed the byteLimit
,
that file and all remaining files will be removed from the directory
.
public static void RemoveFilesBeyondLimit(int byteLimit, string directory, string fileNamePrefix, string fileNameSuffix, bool leaveOneFile, Comparison<FileSystemInfo> sortComparison)
Parameters
byteLimit
intThe maximum total size in bytes allowed for all files that match the file name pattern.
directory
stringThe directory to search for excess files.
fileNamePrefix
stringThe start of the file name pattern, which may be empty.
fileNameSuffix
stringThe end of the file name pattern. For example, the file name extension, which may be empty.
leaveOneFile
boolIf true, the first sorted file will not be removed, even if it exceeds the
byteLimit
.sortComparison
Comparison<FileSystemInfo>The comparison method to use for sorting files, before accumulating their file size.
Exceptions
- ArgumentNullException
Thrown if the
directory
,fileNamePrefix
,fileNameSuffix
, orsortComparison
> are null.- ArgumentException
Thrown if
directory
,fileNamePrefix
, orfileNameSuffix
are Empty. \ OR \ Thrown ifdirectory
contains invalid path characters. \ OR \ Thrown iffileNamePrefix
orfileNameSuffix
contain invalid file name characters, for the current platform. \ OR \ Thrown if the platform is Windows, and thefileNameSuffix
ends with a period or space character.- SecurityException
The caller does not have the required permission to access the directory or files.
- DirectoryNotFoundException
The specified path is invalid.
- UnauthorizedAccessException
The caller does not have the required permission to delete a file.
- PathTooLongException
The specified path exceeds the system-defined maximum length.
- IOException
A file to be deleted is in use.
- AggregateException
Thrown if multiple exceptions occur during the course of this method.