// 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) { // sourceFileName and destFileName validation in Path.GetFullPath() sourceFileName = Path.GetFullPath(sourceFileName); destFileName = Path.GetFullPath(destFileName); bool 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) Object 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 = !NativeIO.Move(sourceFileName, destFileName); } finally { FileSystemManager.RemoveFromOpenList(srcRecord); } if (tryCopyAndDelete) { Copy(sourceFileName, destFileName, false, true); } }
public static void Move(string sourceDirName, string destDirName) { // sourceDirName and destDirName validation in Path.GetFullPath() sourceDirName = Path.GetFullPath(sourceDirName); destDirName = Path.GetFullPath(destDirName); bool tryCopyAndDelete = false; Object srcRecord = FileSystemManager.AddToOpenList(sourceDirName); try { // Make sure sourceDir is actually a directory if (!Exists(sourceDirName)) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } // If Move() returns false, we'll try doing copy and delete to accomplish the move tryCopyAndDelete = !NativeIO.Move(sourceDirName, destDirName); } finally { FileSystemManager.RemoveFromOpenList(srcRecord); } if (tryCopyAndDelete) { RecursiveCopyAndDelete(sourceDirName, destDirName); } }
public static void SetAttributes(String path, FileAttributes fileAttributes) { // path validation in Path.GetFullPath() String fullPath = Path.GetFullPath(path); NativeIO.SetAttributes(fullPath, (uint)fileAttributes); }
//--// public static DirectoryInfo CreateDirectory(string path) { // path validation in Path.GetFullPath() path = Path.GetFullPath(path); /// According to MSDN, Directory.CreateDirectory on an existing /// directory is no-op. NativeIO.CreateDirectory(path); return(new DirectoryInfo(path)); }
public static void Delete(string path, bool recursive) { path = Path.GetFullPath(path); Object record = FileSystemManager.LockDirectory(path); try { uint attributes = NativeIO.GetAttributes(path); if (attributes == 0xFFFFFFFF) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } if (((attributes & (uint)(FileAttributes.Directory)) == 0) || ((attributes & (uint)(FileAttributes.ReadOnly)) != 0)) { /// it's readonly or not a directory throw new IOException("", (int)IOException.IOExceptionErrorCode.UnauthorizedAccess); } if (!Exists(path)) // make sure it is indeed a directory (and not a file) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } if (!recursive) { NativeFindFile ff = new NativeFindFile(path, "*"); try { if (ff.GetNext() != null) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotEmpty); } } finally { ff.Close(); } } NativeIO.Delete(path); } finally { // regardless of what happened, we need to release the directory when we're done FileSystemManager.UnlockDirectory(record); } }
private static void RecursiveCopyAndDelete(String sourceDirName, String destDirName) { String[] files; int filesCount, i; int relativePathIndex = sourceDirName.Length + 1; // relative path starts after the sourceDirName and a path seperator // We have to make sure no one else can modify it (for example, delete the directory and // create a file of the same name) while we're moving Object recordSrc = FileSystemManager.AddToOpenList(sourceDirName); try { // Make sure sourceDir is actually a directory if (!Exists(sourceDirName)) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } // Make sure destDir does not yet exist if (Exists(destDirName)) { throw new IOException("", (int)IOException.IOExceptionErrorCode.PathAlreadyExists); } NativeIO.CreateDirectory(destDirName); files = Directory.GetFiles(sourceDirName); filesCount = files.Length; for (i = 0; i < filesCount; i++) { File.Copy(files[i], Path.Combine(destDirName, files[i].Substring(relativePathIndex)), false, true); } files = Directory.GetDirectories(sourceDirName); filesCount = files.Length; for (i = 0; i < filesCount; i++) { RecursiveCopyAndDelete(files[i], Path.Combine(destDirName, files[i].Substring(relativePathIndex))); } NativeIO.Delete(sourceDirName); } finally { FileSystemManager.RemoveFromOpenList(recordSrc); } }
private const int _defaultCopyBufferSize = 2048; /// Experiment on desktop shows 2k-4k is ideal size perfwise. internal static void Copy(String sourceFileName, String destFileName, bool overwrite, bool deleteOriginal) { // sourceFileName and destFileName validation in Path.GetFullPath() sourceFileName = Path.GetFullPath(sourceFileName); destFileName = Path.GetFullPath(destFileName); FileMode writerMode = (overwrite) ? FileMode.Create : FileMode.CreateNew; FileStream reader = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read, NativeFileStream.BufferSizeDefault); try { using (FileStream writer = new FileStream(destFileName, writerMode, FileAccess.Write, FileShare.None, NativeFileStream.BufferSizeDefault)) { long fileLength = reader.Length; writer.SetLength(fileLength); byte[] buffer = new byte[_defaultCopyBufferSize]; for (; ;) { int readSize = reader.Read(buffer, 0, _defaultCopyBufferSize); if (readSize <= 0) { break; } writer.Write(buffer, 0, readSize); } // Copy the attributes too NativeIO.SetAttributes(destFileName, NativeIO.GetAttributes(sourceFileName)); } } finally { if (deleteOriginal) { reader.DisposeAndDelete(); } else { reader.Dispose(); } } }
// 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); string 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 Object record = FileSystemManager.AddToOpenList(path); try { uint attributes = NativeIO.GetAttributes(folderPath); /// If the folder does not exist or invalid we throw DirNotFound Exception (same as desktop). if (attributes == 0xFFFFFFFF) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } /// Folder exists, lets verify whether the file itself exists. attributes = NativeIO.GetAttributes(path); if (attributes == 0xFFFFFFFF) { // No-op on file not found return; } if ((attributes & (uint)(FileAttributes.Directory | FileAttributes.ReadOnly)) != 0) { /// it's a readonly file or an directory throw new IOException("", (int)IOException.IOExceptionErrorCode.UnauthorizedAccess); } NativeIO.Delete(path); } finally { // regardless of what happened, we need to release the file when we're done FileSystemManager.RemoveFromOpenList(record); } }
public static FileAttributes GetAttributes(String path) { // path validation in Path.GetFullPath() String fullPath = Path.GetFullPath(path); uint attributes = NativeIO.GetAttributes(fullPath); if (attributes == 0xFFFFFFFF) { throw new IOException("", (int)IOException.IOExceptionErrorCode.FileNotFound); } else if (attributes == 0x0) { return(FileAttributes.Normal); } else { return((FileAttributes)attributes); } }
// Tests if a file exists. The result is true if the file // given by the specified path exists; otherwise, the result is // false. Note that if path describes a directory, // Exists will return true. // // Your application must have Read permission for the target directory. // public static bool Exists(String path) { try { // path validation in Path.GetFullPath() path = Path.GetFullPath(path); /// Is this the absolute root? this is not a file. string root = Path.GetPathRoot(path); if (String.Equals(root, path)) { return(false); } else { uint attributes = NativeIO.GetAttributes(path); /// This is essentially file not found. if (attributes == 0xFFFFFFFF) { return(false); } if ((attributes & (uint)FileAttributes.Directory) == 0) { /// Not a directory, it must be a file. return(true); } } } catch (Exception) { /// Like desktop, exists here does not throw exception in /// a number of cases, instead returns false. For more /// details see MSDN. } return(false); }
public static bool Exists(string path) { // path validation in Path.GetFullPath() path = Path.GetFullPath(path); /// Is this the absolute root? this always exists. if (path == NativeIO.FSRoot) { return(true); } else { try { uint attributes = NativeIO.GetAttributes(path); /// This is essentially file not found. if (attributes == 0xFFFFFFFF) { return(false); } /// Need to make sure these are not FAT16 or FAT32 specific. if ((((FileAttributes)attributes) & FileAttributes.Directory) == FileAttributes.Directory) { /// It is a directory. return(true); } } catch (Exception) { return(false); } } return(false); }