// Deletes a file. The file specified by the designated path is deleted. // If the file does not exist, Delete succeeds without throwing // an exception. // // On NT, Delete will fail for a file that is open for normal I/O // or a file that is memory mapped. On Win95, the file will be // deleted irregardless of whether the file is being used. // // Your application must have Delete permission to the target file. // public static void Delete(string path) { // path validation in Path.GetFullPath() path = Path.GetFullPath(path); var folderPath = Path.GetDirectoryName(path); // We have to make sure no one else has the file opened, and no one else can modify it when we're deleting var record = FileSystemManager.AddToOpenList(path); try { var attributes = DriveInfo.GetForPath(path).GetAttributes(folderPath); /// If the folder does not exist or invalid we throw DirNotFound Exception (same as desktop). if ((uint)attributes == 0xFFFFFFFF) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } /// Folder exists, lets verify whether the file itself exists. attributes = DriveInfo.GetForPath(path).GetAttributes(path); if ((uint)attributes == 0xFFFFFFFF) { // No-op on file not found return; } if ((attributes & (FileAttributes.Directory | FileAttributes.ReadOnly)) != 0) { /// it's a readonly file or an directory throw new IOException("", (int)IOException.IOExceptionErrorCode.UnauthorizedAccess); } DriveInfo.GetForPath(path).Delete(path); } finally { // regardless of what happened, we need to release the file when we're done FileSystemManager.RemoveFromOpenList(record); } }
// Moves a specified file to a new location and potentially a new file name. // This method does work across volumes. // // The caller must have certain FileIOPermissions. The caller must // have Read and Write permission to // sourceFileName and Write // permissions to destFileName. // public static void Move(string sourceFileName, string destFileName) { if (Path.GetPathRoot(sourceFileName) != Path.GetPathRoot(destFileName)) { throw new ArgumentException(); } // sourceFileName and destFileName validation in Path.GetFullPath() sourceFileName = Path.GetFullPath(sourceFileName); destFileName = Path.GetFullPath(destFileName); var tryCopyAndDelete = false; // We only need to lock the source, not the dest because if dest is taken // Move() will failed at the driver's level anyway. (there will be no conflict even if // another thread is creating dest, as only one of the operations will succeed -- // the native calls are atomic) var srcRecord = FileSystemManager.AddToOpenList(sourceFileName); try { if (!Exists(sourceFileName)) { throw new IOException("", (int)IOException.IOExceptionErrorCode.FileNotFound); } //We'll try copy and deleting if Move returns false tryCopyAndDelete = !DriveInfo.GetForPath(sourceFileName).Move(sourceFileName, destFileName); } finally { FileSystemManager.RemoveFromOpenList(srcRecord); } if (tryCopyAndDelete) { Copy(sourceFileName, destFileName, false, true); } }