2012/02/07

C# big file copy

0 comments
        public static void Copy(Stream input, string targetFile, int length)
        {
            byte[] buffer = new byte[8192];
            using (Stream output = File.OpenWrite(targetFile))
            {
                int bytesRead = 1;
                while (length > 0 && bytesRead > 0)
                {
                    bytesRead = input.Read(buffer, 0, Math.Min(length, buffer.Length));
                    output.Write(buffer, 0, bytesRead);
                    length -= bytesRead;
                }
            }
        }

        public static void CopyFile(string sourceFile, string targetFile)
        {
            if (File.Exists(targetFile))
            {
                File.Delete(targetFile);
            }
            Stream input = File.OpenRead(sourceFile);
            Copy(input, targetFile, (int)input.Length);
            System.GC.Collect();
        }

0 comments:

Post a Comment

 

Khanh DAO | © 2011

Design by DheTemplate.com and Theme 2 Blog