public static void SetAttributes(String path, FileAttributes fileAttributes) { // path validation in Path.GetFullPath() String fullPath = Path.GetFullPath(path); NativeIO.SetAttributes(fullPath, (uint)fileAttributes); }
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(); } } }