使用FileStream实现文件的复制

    技术2025-07-11  9

    namespace _07_File和FileStream { class Program { static void Main(string[] args) { string sourcePath = @"C:\Users\Gerton\Desktop\源文件.mp3"; string targetPath = @"C:\Users\Gerton\Desktop\目标文件.mp3"; myCopy(sourcePath, targetPath); Console.ReadKey(); } public static void myCopy(string sourcePath,string targetPath) { using (FileStream fileRead = new FileStream(sourcePath, FileMode.Open, FileAccess.Read)) { using(FileStream fileWrite = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] buffer = new byte[1024 * 1024]; while (true) { int r = fileRead.Read(buffer, 0, buffer.Length); if (r == 0) { break; } fileWrite.Write(buffer, 0, r); } } } } } }
    Processed: 0.011, SQL: 9