將文件復制到指定路徑[C# 文件操作]


將現有文件復制到新文件,不允許改寫現有文件。[C#] public FileInfo CopyTo(string);

將現有文件復制到新文件,允許改寫現有文件。[C#] public FileInfo CopyTo(string, bool);
[C#]  將文件復制到指定路徑,允許改寫同名的目標文件 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 the 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 was 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 the target does not exist.
            fi2.Delete();

            //Copy the file.
            fi1.CopyTo(path2);
            Console.WriteLine("{0} was 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 was not allowed, which is not expected.");
        }
    }
}




若要執行此操作... 請參見本主題中的示例...
創建文本文件。  
寫入文本文件。  
讀取文本文件。  
向文件中追加文本。

File.AppendText

FileInfo.AppendText

重命名或移動文件。 File.Move

FileInfo.MoveTo

刪除文件。 File.Delete

FileInfo.Delete

復制文件。 File.Copy

FileInfo.CopyTo

獲取文件大小。 FileInfo.Length
獲取文件屬性。 File.GetAttributes
設置文件屬性。 File.SetAttributes
確定文件是否存在。 File.Exists
讀取二進制文件。  
寫入二進制文件。  
檢索文件擴展名。 Path.GetExtension
檢索文件的完全限定路徑。 Path.GetFullPath
檢索路徑中的文件名和擴展名。 Path.GetFileName
更改文件擴展名。 Path.ChangeExtension



Progress 類的使用
private void CopyWithProgress(string[] filenames)
{
    // Display the ProgressBar control.
    pBar1.Visible = true;
    // Set Minimum to 1 to represent the first file 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 property 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();
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM