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