C # file Copy,

Source: Internet
Author: User

C # file Copy,

File Copy can be performed in the following ways:

1. Copy

String sourceFile = @ "c: \ temp \ New Text Document.txt"; string destinationFile = @ "c: \ temp \ test.txt"; bool isrewrite = true; // true = overwrite an existing file with the same name. false indicates that the file is replaced by System. IO. file. copy (sourcePath, targetPath, isrewrite );

 

2. CopyTo

string sourceFile = @"c:\temp\New Text Document.txt"; string destinationFile = @"c:\temp\test.txt"; FileInfo file = new FileInfo(sourceFile); if (file.Exists) {     // true is overwrite     file.CopyTo(destinationFile, true); } 

 

3. Use file stream read/write to implement Copy

# Region copy operation private void button3_Click (object sender, EventArgs e) {OpenFileDialog ofd = new OpenFileDialog (); DialogResult res = ofd. showDialog (); if (res = DialogResult. OK) {if (! String. isNullOrEmpty (ofd. fileName) {// 1. create a file stream object for reading FileStream streamRead = new FileStream (ofd. fileName, FileMode. open); // 2. create an array of 1 bytes to receive the value of the file stream object read operation byte [] data = new byte [1024*1024]; // 1 M int length = 0; saveFileDialog sfd = new SaveFileDialog (); DialogResult sres = sfd. showDialog (); if (sres = DialogResult. OK) {if (! String. isNullOrEmpty (ofd. fileName) {FileStream streamWrite = new FileStream (sfd. fileName, FileMode. create); do {// 3. parameters of the file stream read method 1. data-where the file stream reads data, 2. 0 -- from where to read, 3. data. length -- how many bytes of data are Read once // the return value of the 3.1 Read method is an int type, which indicates the length of the bytes actually Read. Length = streamRead. read (data, 0, data. length); // when reading a large file, we may define a limited byte Length. If the file is too large, we need to receive the Read () method of the file stream object, returns the actual read length // encryption and decryption for (int I = 0; I <length; I ++) {data [I] = (byte) (255-data [I]);} streamWrite. write (data, 0, length);} while (length = data. length); // if the actual write Length is equal to the Length we set, there are two cases: 1. the file is exactly the length we set. 2. the file is oversized and only part of the screenshot is uploaded. }}}# endregion

 

This article cited from: http://www.cnblogs.com/WarBlog/p/5826778.html

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.