C # copying large files,
How to copy and read large files may be confusing for many people for a long time. I don't know how to do this. It is really a headache. Once you know it, you will find it so simple, go straight to the topic ''''
Static void Main (string [] args) {bool B = CopyFile (@ "D: \ 360 secure Browser Download \ 2.avi", @" D: \ 360 secure Browser Download \ 3.avi "); if (B) {Console. writeLine ("copied");} else {Console. writeLine ("failed to copy");} Console. readKey () ;}//< summary> /// copy a large file multiple times. true: copy successful. false: failed to copy /// </summary> /// <param name = "soucrePath"> original file path </param> /// <param name = "targetPath"> copy target file Path </param> /// <returns> </returns> public static bool CopyFile (string soucrePath, string targetPath) {try {// read the copy file stream using (FileStream fsRead = new FileStream (soucrePath, FileMode. open, FileAccess. read) {// write file copy stream using (FileStream fsWrite = new FileStream (targetPath, FileMode. openOrCreate, FileAccess. write) {byte [] buffer = new byte [1024*1024*2]; // read 2 MB at a time. // the file may be large and should be read cyclically, 2 M while (true) read each time {// data read each time n: the actual data size read each time int n = fsRead. read (buffer, 0, buffer. count (); // if n = 0, the read data is null and has been read to the end. if (n = 0) {break ;} // write the actual data size that is read each time fsWrite. write (buffer, 0, n) ;}} return true;} catch (System. exception ex) {return false ;}}
It is actually a method CopyFile (). You can call this method ······