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
byteLimitintThe maximum total size in bytes allowed for all files that match the file name pattern.
directorystringThe directory to search for excess files.
fileNamePrefixstringThe start of the file name pattern, which may be empty.
fileNameSuffixstringThe end of the file name pattern. For example, the file name extension, which may be empty.
Exceptions
- ArgumentNullException
Thrown if the
directory,fileNamePrefix, orfileNameSuffixare null.- ArgumentException
Thrown if
directory,fileNamePrefix, orfileNameSuffixare Empty. \ OR \ Thrown ifdirectorycontains invalid path characters. \ OR \ Thrown iffileNamePrefixorfileNameSuffixcontain invalid file name characters, for the current platform. \ OR \ Thrown if the platform is Windows, and thefileNameSuffixends 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
byteLimitintThe maximum total size in bytes allowed for all files that match the file name pattern.
directorystringThe directory to search for excess files.
fileNamePrefixstringThe start of the file name pattern, which may be empty.
fileNameSuffixstringThe end of the file name pattern. For example, the file name extension, which may be empty.
leaveOneFileboolIf true, the first sorted file will not be removed, even if it exceeds the
byteLimit.
Exceptions
- ArgumentNullException
Thrown if the
directory,fileNamePrefix, orfileNameSuffixare null.- ArgumentException
Thrown if
directory,fileNamePrefix, orfileNameSuffixare Empty. \ OR \ Thrown ifdirectorycontains invalid path characters. \ OR \ Thrown iffileNamePrefixorfileNameSuffixcontain invalid file name characters, for the current platform. \ OR \ Thrown if the platform is Windows, and thefileNameSuffixends 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
byteLimitintThe maximum total size in bytes allowed for all files that match the file name pattern.
directorystringThe directory to search for excess files.
fileNamePrefixstringThe start of the file name pattern, which may be empty.
fileNameSuffixstringThe end of the file name pattern. For example, the file name extension, which may be empty.
leaveOneFileboolIf true, the first sorted file will not be removed, even if it exceeds the
byteLimit.sortComparisonComparison<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, orfileNameSuffixare Empty. \ OR \ Thrown ifdirectorycontains invalid path characters. \ OR \ Thrown iffileNamePrefixorfileNameSuffixcontain invalid file name characters, for the current platform. \ OR \ Thrown if the platform is Windows, and thefileNameSuffixends 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.