io/filestream-ex/file-copy/copy-file-1.cs - A FileCopy method in a source file copy-file.cs - uses two FileStreams. | Lecture 10 - slide 8 : 40 Program 1 |
using System; using System.IO; public class CopyApp { public static void Main(string[] args) { FileCopy(args[0], args[1]); } public static void FileCopy(string fromFile, string toFile){ try{ using(FileStream fromStream = new FileStream(fromFile, FileMode.Open)){ using(FileStream toStream = new FileStream(toFile, FileMode.Create)){ int c; do{ c = fromStream.ReadByte(); if(c != -1) toStream.WriteByte((byte)c); } while (c != -1); } } } catch(FileNotFoundException e){ Console.WriteLine("File {0} not found: ", e.FileName); throw; } catch(Exception){ Console.WriteLine("Other file copy exception"); throw; } } }