Table of Contents

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 int

The maximum total size in bytes allowed for all files that match the file name pattern.

directory string

The directory to search for excess files.

fileNamePrefix string

The start of the file name pattern, which may be empty.

fileNameSuffix string

The end of the file name pattern. For example, the file name extension, which may be empty.

Exceptions

ArgumentNullException

Thrown if the directory, fileNamePrefix, or fileNameSuffix are null.

ArgumentException

Thrown if directory, fileNamePrefix, or fileNameSuffix are Empty. \ OR \ Thrown if directory contains invalid path characters. \ OR \ Thrown if fileNamePrefix or fileNameSuffix contain invalid file name characters, for the current platform. \ OR \ Thrown if the platform is Windows, and the fileNameSuffix 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 int

The maximum total size in bytes allowed for all files that match the file name pattern.

directory string

The directory to search for excess files.

fileNamePrefix string

The start of the file name pattern, which may be empty.

fileNameSuffix string

The end of the file name pattern. For example, the file name extension, which may be empty.

leaveOneFile bool

If true, the first sorted file will not be removed, even if it exceeds the byteLimit.

Exceptions

ArgumentNullException

Thrown if the directory, fileNamePrefix, or fileNameSuffix are null.

ArgumentException

Thrown if directory, fileNamePrefix, or fileNameSuffix are Empty. \ OR \ Thrown if directory contains invalid path characters. \ OR \ Thrown if fileNamePrefix or fileNameSuffix contain invalid file name characters, for the current platform. \ OR \ Thrown if the platform is Windows, and the fileNameSuffix 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 int

The maximum total size in bytes allowed for all files that match the file name pattern.

directory string

The directory to search for excess files.

fileNamePrefix string

The start of the file name pattern, which may be empty.

fileNameSuffix string

The end of the file name pattern. For example, the file name extension, which may be empty.

leaveOneFile bool

If 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, or sortComparison> are null.

ArgumentException

Thrown if directory, fileNamePrefix, or fileNameSuffix are Empty. \ OR \ Thrown if directory contains invalid path characters. \ OR \ Thrown if fileNamePrefix or fileNameSuffix contain invalid file name characters, for the current platform. \ OR \ Thrown if the platform is Windows, and the fileNameSuffix 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.