From 99c3e77e8fbfe68f5dca5d74c4abc36de412ab5d Mon Sep 17 00:00:00 2001 From: Michiel Oda Date: Tue, 4 Jun 2024 11:50:50 +0200 Subject: [PATCH 1/2] Add the proper methods for File.Delete & File.Move No need to change code when using FileSystem instead of System.IO. --- FileSystem/FileIOLinux.cs | 13 +++++++++++++ FileSystem/FileIOWin.cs | 13 +++++++++++++ FileSystem/IFileIO.cs | 41 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/FileSystem/FileIOLinux.cs b/FileSystem/FileIOLinux.cs index d7976c6..095bcd0 100644 --- a/FileSystem/FileIOLinux.cs +++ b/FileSystem/FileIOLinux.cs @@ -24,6 +24,13 @@ public void DeleteFile(string path) } } + /// + public void Delete(string path) + { + TryAllowWritesOnFile(path); + File.Delete(path); + } + /// public bool Exists(string path) { @@ -93,6 +100,12 @@ public void MoveFile(string filePath, string sourceFolder, string targetFolder, } } + /// + public void Move(string sourceFileName, string destFileName) + { + File.Move(sourceFileName, destFileName); + } + /// public byte[] ReadAllBytes(string filePath) { diff --git a/FileSystem/FileIOWin.cs b/FileSystem/FileIOWin.cs index 033a81d..b8bf7ae 100644 --- a/FileSystem/FileIOWin.cs +++ b/FileSystem/FileIOWin.cs @@ -27,6 +27,13 @@ public void DeleteFile(string path) } } + /// + public void Delete(string path) + { + TryAllowWritesOnFile(path); + File.Delete(path); + } + /// public bool Exists(string path) { @@ -96,6 +103,12 @@ public void MoveFile(string filePath, string sourceFolder, string targetFolder, } } + /// + public void Move(string sourceFileName, string destFileName) + { + File.Move(sourceFileName, destFileName); + } + /// public byte[] ReadAllBytes(string filePath) { diff --git a/FileSystem/IFileIO.cs b/FileSystem/IFileIO.cs index 375bb54..3ee5bd8 100644 --- a/FileSystem/IFileIO.cs +++ b/FileSystem/IFileIO.cs @@ -23,10 +23,32 @@ public interface IFileIO void AppendAllText(string filePath, string fileContent); /// - /// Deletes the specified file. + /// Deletes the specified file. Won't fail if the location does not exist. /// /// Will delete a file. void DeleteFile(string path); + + /// Deletes the specified file. + /// The name of the file to be deleted. Wildcard characters are not supported. + /// + /// is a zero-length string, contains only white space, or contains one or more invalid characters as defined by . + /// + /// is . + /// The specified path is invalid (for example, it is on an unmapped drive). + /// The specified file is in use. + /// -or- + /// There is an open handle on the file, and the operating system is Windows XP or earlier. This open handle can result from enumerating directories and files. For more information, see How to: Enumerate Directories and Files. + /// + /// is in an invalid format. + /// The specified path, file name, or both exceed the system-defined maximum length. + /// The caller does not have the required permission. + /// -or- + /// The file is an executable file that is in use. + /// -or- + /// is a directory. + /// -or- + /// specified a read-only file. + void Delete(string path); /// Determines whether the specified file exists. /// The file to check. @@ -78,6 +100,23 @@ public interface IFileIO /// Forces a replacement in case the file exists in the target folder. void MoveFile(string filePath, string sourceFolder, string targetFolder, bool forceReplace); + /// Moves a specified file to a new location, providing the option to specify a new file name. + /// The name of the file to move. Can include a relative or absolute path. + /// The new path and name for the file. + /// The destination file already exists. + /// -or- + /// was not found. + /// + /// or is . + /// + /// or is a zero-length string, contains only white space, or contains invalid characters as defined in . + /// The caller does not have the required permission. + /// The specified path, file name, or both exceed the system-defined maximum length. + /// The path specified in or is invalid, (for example, it is on an unmapped drive). + /// + /// or is in an invalid format. + void Move(string sourceFileName, string destFileName); + /// /// Opens a binary file, reads the contents of the file into a byte array, and then closes the file. /// From eaa27313589d4b1708c4e524a2bc60805e184d78 Mon Sep 17 00:00:00 2001 From: Michiel Oda Date: Tue, 11 Jun 2024 08:59:06 +0200 Subject: [PATCH 2/2] CR remarks --- FileSystem/FileIOLinux.cs | 3 +-- FileSystem/FileIOWin.cs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/FileSystem/FileIOLinux.cs b/FileSystem/FileIOLinux.cs index 095bcd0..0427c4e 100644 --- a/FileSystem/FileIOLinux.cs +++ b/FileSystem/FileIOLinux.cs @@ -19,8 +19,7 @@ public void DeleteFile(string path) { if (File.Exists(path)) { - TryAllowWritesOnFile(path); - File.Delete(path); + Delete(path); } } diff --git a/FileSystem/FileIOWin.cs b/FileSystem/FileIOWin.cs index b8bf7ae..fdbde11 100644 --- a/FileSystem/FileIOWin.cs +++ b/FileSystem/FileIOWin.cs @@ -22,8 +22,7 @@ public void DeleteFile(string path) { if (File.Exists(path)) { - TryAllowWritesOnFile(path); - File.Delete(path); + Delete(path); } }