The FileHandler class provides an abstraction for managing and interacting with files in a secure and structured way. It includes functionalities for reading, writing, appending, and managing file properties. It also leverages FileIOPermission to check for access permissions.
namespace fileHanding- System.IO: For file operations.
- System.Security.Permissions: For permission checks.
FileHandler(string path, string mode, bool createIfNotFound = true)
Initializes a new instance of the FileHandler class.
path(string): The file path to manage.mode(string): The mode in which the file is to be handled.logpath(string): The path to where the logs are saved.createIfNotFound(bool): Whether to create the file if it doesn't exist. Defaults totrue.
logPath(string): Reserved for logging purposes.path(string): The file path being managed.mode(string): The operation mode of the file.interactor(FileInteractor): Handles file-specific operations.createIfNotFound(bool): Determines if the file should be created when missing.properties(Dictionary<string, string>): Stores file metadata.fileInfo(FileInfo): Stores information about the file.
Returns the stored file properties such as size, creation time, last access time, and last edit time.
Checks if the file is readable.
Checks if the file is writable.
Checks if the file can be appended to.
Note: Each method uses FileIOPermission to verify access.
Appends a list of lines to the file. Optionally allows toggling line-by-line writing.
Overwrites the file with the provided list of lines. Optionally allows toggling line-by-line writing.
Reads the file and returns its content as an array of strings.
Reads the file and returns its content as a 2D array of strings, should it be a CSV file.
Creates the file if it doesn't exist.
Deletes the file.
Moves the file to a new location and updates the file path.
Creates a copy of the file at the specified location.
Takes the metadata of the file (size, creation time, last access time, etc.) and stores it in the properties dictionary.
FileIOPermission: Ensures secure file access by demanding appropriate permissions for read, write, and append operations.- Error Handling: Exception handling should be added when deploying in production environments to handle unauthorized access or file-related errors gracefully.