<p>Anybody know of a way to copy a file from path A to path B and suppressing the Windows file system cache?<br>
Typical use is copying a large file from a USB drive, or server to your local machine. Windows seems to swap everything out if the file is really big, e.g. 2GiB.
Prefer example in C#, but I'm guessing this would be a Win32 call of some sort if possible.</p>

[{'answer_id': 48683, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 2, 'selected': False, 'text': '<p>I am not sure if this helps, but take a look at <a href="http://support.microsoft.com/default.aspx?scid=kb;EN-US;98756" rel="nofollow noreferrer">Increased Performance Using FILE_FLAG_SEQUENTIAL_SCAN</a>.</p>\n\n<blockquote>\n  <p>SUMMARY</p>\n  \n  <p>There is a flag for CreateFile()\n  called FILE_FLAG_SEQUENTIAL_SCAN which\n  will direct the Cache Manager to\n  access the file sequentially. </p>\n  \n  <p>Anyone reading potentially large files\n  with sequential access can specify\n  this flag for increased performance.\n  This flag is useful if you are reading\n  files that are "mostly" sequential,\n  but you occasionally skip over small\n  ranges of bytes.</p>\n</blockquote>\n'}, {'answer_id': 48689, 'author': 'gabr', 'author_id': 4997, 'author_profile': 'https://Stackoverflow.com/users/4997', 'pm_score': 4, 'selected': True, 'text': '<p>Even more important, there are FILE_FLAG_WRITE_THROUGH and FILE_FLAG_NO_BUFFERING.</p>\n\n<p>MSDN has a nice article on them both: <a href="http://support.microsoft.com/kb/99794" rel="noreferrer">http://support.microsoft.com/kb/99794</a></p>\n'}, {'answer_id': 48704, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 2, 'selected': False, 'text': '<p>If you dont mind using a tool, ESEUTIL worked great for me.</p>\n\n<p>You can check out this <a href="http://blogs.technet.com/askperf/archive/2007/05/08/slow-large-file-copy-issues.aspx" rel="nofollow noreferrer">blog</a> entry comparing Buffered and NonBuffered IO functions and from where to get ESEUTIL.</p>\n\n<p>copying some text from the technet blog:</p>\n\n<p><em>So looking at the definition of buffered I/O above, we can see where the perceived performance problems lie - in the file system cache overhead.  Unbuffered I/O (or a raw file copy) is preferred when attempting to copy a large file from one location to another when we do not intend to access the source file after the copy is complete.  This will avoid the file system cache overhead and prevent the file system cache from being effectively flushed by the large file data.  Many applications accomplish this by calling CreateFile() to create an empty destination file, then using the ReadFile() and WriteFile() functions to transfer the data.\nCreateFile() - The CreateFile function creates or opens a file, file stream, directory, physical disk, volume, console buffer, tape drive, communications resource, mailslot, or named pipe. The function returns a handle that can be used to access an object. \nReadFile() - The ReadFile function reads data from a file, and starts at the position that the file pointer indicates. You can use this function for both synchronous and asynchronous operations. \nWriteFile() - The WriteFile function writes data to a file at the position specified by the file pointer. This function is designed for both synchronous and asynchronous operation.\nFor copying files around the network that are very large, my copy utility of choice is ESEUTIL which is one of the database utilities provided with Exchange.</em> </p>\n'}, {'answer_id': 6198881, 'author': 'nietras', 'author_id': 98692, 'author_profile': 'https://Stackoverflow.com/users/98692', 'pm_score': 3, 'selected': False, 'text': '<p>In C# I have found something like this to work, this can be changed to copy directly to destination file:</p>\n\n<pre><code>    public static byte[] ReadAllBytesUnbuffered(string filePath)\n    {\n        const FileOptions FileFlagNoBuffering = (FileOptions)0x20000000;\n        var fileInfo = new FileInfo(filePath);\n        long fileLength = fileInfo.Length;\n        int bufferSize = (int)Math.Min(fileLength, int.MaxValue / 2);\n        bufferSize += ((bufferSize + 1023) &amp; ~1023) - bufferSize;\n        using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None,\n                                           bufferSize, FileFlagNoBuffering | FileOptions.SequentialScan))\n        {\n            long length = stream.Length;\n            if (length &gt; 0x7fffffffL)\n            {\n                throw new IOException("File too long over 2GB");\n            }\n            int offset = 0;\n            int count = (int)length;\n            var buffer = new byte[count];\n            while (count &gt; 0)\n            {\n                int bytesRead = stream.Read(buffer, offset, count);\n                if (bytesRead == 0)\n                {\n                    throw new EndOfStreamException("Read beyond end of file EOF");\n                }\n                offset += bytesRead;\n                count -= bytesRead;\n            }\n            return buffer;\n        }\n    }\n</code></pre>\n'}, {'answer_id': 13661822, 'author': 'Michael Böckling', 'author_id': 58962, 'author_profile': 'https://Stackoverflow.com/users/58962', 'pm_score': 0, 'selected': False, 'text': '<p>Eseutil is a correct answer, also since Win7 / 2008 R2, you can use the /j switch in Xcopy, which has the same effect.</p>\n'}, {'answer_id': 58746922, 'author': 'Petros Matsakos', 'author_id': 4793294, 'author_profile': 'https://Stackoverflow.com/users/4793294', 'pm_score': 0, 'selected': False, 'text': '<p>I understand this question was 11 years ago, nowadays there is robocopy which is kind of replacement for xcopy.</p>\n\n<p>you need to check /J option\n<code>/J :: copy using unbuffered I/O (recommended for large files)</code></p>\n'}]
['https://Stackoverflow.com/questions/48679', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5080/']