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 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); }