C#如何:複製、刪除和移動檔案和檔案夾

來源:互聯網
上載者:User

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


如何:複製、刪除和移動檔案和檔案夾


Visual Studio 2013 其他版本

以下樣本說明如何使用 System.IO 命名空間中的 System.IO.File、System.IO.Directory、System.IO.FileInfo 和 System.IO.DirectoryInfo 類以同步方式複製、移動和刪除檔案和檔案夾。 這些樣本沒有提供進度列或其他任何使用者介面。 如果您想提供一個標準進度對話方塊,請參見如何:提供檔案操作進度對話方塊(C# 編程指南)。

在操作多個檔案時,請使用 System.IO.FileSystemWatcher 提供一些事件,以便可以利用這些事件計算進度。 另一種方法是使用平台叫用來調用 Windows Shell 中相應的檔案相關方法。 有關如何非同步執行這些檔案操作的資訊,請參見非同步檔案 I/O。 樣本

下面的樣本示範如何複製檔案和目錄。 C#

// Simple synchronous file copy operations with no user interface.// To run this sample, first create the following directories and files:// C:\Users\Public\TestFolder// C:\Users\Public\TestFolder\test.txt// C:\Users\Public\TestFolder\SubDir\test.txtpublic class SimpleFileCopy{    static void Main()    {        string fileName = "test.txt";        string sourcePath = @"C:\Users\Public\TestFolder";        string targetPath =  @"C:\Users\Public\TestFolder\SubDir";        // Use 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 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, see        // "How to: Iterate Through a Directory Tree.")        // Note: Check for target path was 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 the 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();    }}

下面的樣本示範如何移動檔案和目錄。 C#

// Simple synchronous 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");    }}

下面的樣本示範如何刪除檔案和目錄。 C#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.