How C #: Copy, delete, and move files and folders

Source: Internet
Author: User
Tags file copy


https://msdn.microsoft.com/zh-cn/library/cc148994


How to: Copy, delete, and move files and folders


Visual Studio 2013 Other versions

The following example shows how to use the System.IO.File, System.IO.Directory, System.IO.FileInfo, and System.IO.DirectoryInfo classes in the System.IO namespace to replicate synchronously , move, and delete files and folders. These examples do not provide a progress bar or any other user interface. If you want to provide a standard progress dialog box, see How to: Provide a file Operation progress dialog box (C # Programming Guide).

When you are working with multiple files, use System.IO.FileSystemWatcher to provide some events so that you can use these events to calculate progress. Another approach is to use platform invoke to call the appropriate file-related methods in the Windows Shell. For information about how to perform these file operations asynchronously, see Asynchronous file I/O. Sample

The following example shows how to copy files and directories. C#

Simple synchronous file copy operations with no user interface. To run this sample, the following directories and files://C:\Users\Public\TestFolder//C:\Users\Public\
    Testfolder\test.txt//C:\Users\Public\TestFolder\SubDir\test.txt public class Simplefilecopy {static void Main ()
        {String fileName = ' test.txt ';
        String SourcePath = @ "C:\Users\Public\TestFolder";

        String TargetPath = @ "C:\Users\Public\TestFolder\SubDir";
        Use the Path class to manipulate file and directory paths.
        String sourcefile = System.IO.Path.Combine (SourcePath, fileName);

        String destfile = System.IO.Path.Combine (TargetPath, fileName);
        To copy a folder ' s contents to a new location://Create A new target folder, if necessary. if (!
        System.IO.Directory.Exists (TargetPath)) {System.IO.Directory.CreateDirectory (TargetPath); }//To copy a, file to another location and//Overwrite the destination file if it already exists.

        System.IO.File.Copy (SourceFile, DestFile, true);
        To copy all of the files in one directory to another directory. Get the files in the source folder. (to recursively iterate through//all subfolders under the current directory, click//"How To:iterate Thr
        Ough a Directory tree. "
        Note:check for Target path is performed previously//in this code example. if (System.IO.Directory.Exists (SourcePath)) {string[] files = System.IO.Directory.GetFiles (sourcepath

            );
            Copy the files and overwrite destination files if they already exist. foreach (string s in files) {//Use static Path methods to extract only the ' file name from th
                E path.
                FileName = System.IO.Path.GetFileName (s);
                DestFile = System.IO.Path.Combine (TargetPath, fileName); System.IO.FiLe.
            Copy (S, DestFile, true);
        } else {Console.WriteLine ("Source Path does not exist!");
        }//Keep console window open in debug mode.
        Console.WriteLine ("Press any key to exit.");
    Console.readkey ();


 }
}

The following example shows how to move files and directories. C#

Simple synchronous the file move operations with no user interface.
public class Simplefilemove
{
    static void Main ()
    {
        string sourcefile = @ "C:\Users\Public\public\ Test.txt ";
        String destinationfile = @ "C:\Users\Public\private\test.txt";

        To move a file or folder to a new location:
        System.IO.File.Move (SourceFile, destinationfile);

        To move an entire directory. To programmatically modify or combine
        //path strings, use the System.IO.Path class.
        System.IO.Directory.Move (@ "C:\Users\Public\public\test\", @ "C:\Users\Public\private");
    }



The following example shows how to delete files and directories. C#

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.