Copy files to the specified path [C # file Operations]

Source: Internet
Author: User

Copying an existing file to a new file does not allow overwriting of existing files. [C #] public FileInfo CopyTo (string);

Copies an existing file to a new file, allowing overwriting of an existing file. [C #] public FileInfo CopyTo (string, bool);
[C #] copies files to the specified path, allowing overwriting of target files with the same name copy
Using System;
Using System.IO;

Class Test
{
public static void Main ()
{
string path = @ "C:/temp/mytest.txt";
String path2 = path + "Temp";

Try
{
Create the file and clean up handles.
using (FileStream fs = file.create (path)) {}

Ensure that's target does not exist.
File.delete (path2);

Copy the file.
File.Copy (path, path2);
Console.WriteLine ("{0} copied to {1}", path, path2);

Try to copy the same file again, which should succeed.
File.Copy (Path, path2, true);
Console.WriteLine ("The second Copy operation succeeded, which was expected.");
}

Catch
{
Console.WriteLine ("Double copy is not allowed, which be not expected.");
}
}
}
[C #] COPY to
Using System;
Using System.IO;

Class Test
{
public static void Main ()
{
string path = @ "C:/temp/mytest.txt";
String path2 = @ "C:/temp/mytest.txt" + "temp";
FileInfo fi1 = new FileInfo (path);
FileInfo fi2 = new FileInfo (path2);

Try
{
Create the file and clean up handles.
using (FileStream fs = Fi1. Create ()) {}

Ensure that's target does not exist.
Fi2. Delete ();

Copy the file.
Fi1. CopyTo (path2);
Console.WriteLine ("{0} is copied to {1}.", Path, path2);

Try to copy it again, which should succeed.
Fi1. CopyTo (path2, true);

Console.WriteLine ("The second Copy operation succeeded, which is expected.");

}
Catch
{
Console.WriteLine ("Double copying being not allowed, which are not expected.");
}
}
}




to perform this action ... See the example in this topic ...
Create a text file. ? Xml:namespace PREFIX = mshelp/> Write text to file
Write to a text file. Writing text to a file
Reads a text file. Reading text from a file
Appends text to the file. Open and attach to a log file

File.appendtext

Fileinfo.appendtext

Renames or moves the file. File.move

Fileinfo.moveto

Deletes a file. File.delete

Fileinfo.delete

Copy the file. File.Copy

Fileinfo.copyto

Gets the file size. Fileinfo.length
Gets the file properties. File.getattributes
Sets the file properties. File.setattributes
Determine if the file exists. File.exists
Read binary files. Read and write to the data file you just created
Writes a binary file. Read and write to the data file you just created
Retrieves the file name extension. Path.getextension
Retrieves the fully qualified path to a file. Path.GetFullPath
Retrieves the file name and extension in the path. Path.getfilename
Change the file name extension. Path.changeextension


Use of the Progress class
private void Copywithprogress (string[] filenames)
{
Display the ProgressBar control.
Pbar1.visible = true;
Set Minimum to 1 to represent the "a"-being copied.
Pbar1.minimum = 1;
Set Maximum to the total number of files to copy.
Pbar1.maximum = filenames. Length;
Set the initial value of the ProgressBar.
Pbar1.value = 1;
Set the "step" to a value of 1 to represent each file

being copied.
    pbar1.step = 1;
               //Loop through all Files to copy.
    for (int x = 1; x <= filenames. Length; x + +)
    {
       //Copy the file and increment the ProgressBar if successful.
        if (copyfile (filenames[x-1) = = True)
         {
           //Perform the increment On the ProgressBar.
            pbar1.performstep ();
       }
   }
}

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.