C # copying large files,

Source: Internet
Author: User

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 ······

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.