The text of this article is not in this database — only its details are. Read it on the old site: FileStream in C#
3 Comments
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
The text of this article is not in this database — only its details are. Read it on the old site: FileStream in C#
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
Sanil BhosalePosted Jun 11, 2020, 9:27 AM
Static void Main(string[] args) { if (args.Length < 2) { //Show usage information if incorrect number //of parameters has been entered Console.WriteLine("Usage: FileCopy source destination"); return; } try { //Open a FileStream to the source file FileStream fin = new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.Read); //Open a FileStream to the destination file FileStream fout = new FileStream(args[1], FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); //Create a byte array to act as a buffer Byte[] buffer = new Byte[32]; Console.WriteLine("File Copy Started"); //Loop until end of file is not reached while (fin.Position != fin.Length) { //Read from the source file //The Read method returns the number of bytes read int n = fin.Read(buffer, 0, buffer.Length); //Write the contents of the buffer to the destination file fout.Write(buffer, 0, n); } //Flush the contents of the buffer to the file fout.Flush(); //Close the streams and free the resources fin.Close(); fout.Close(); Console.WriteLine("File Copy Ended"); } catch (IOException e) { //Catch a IOException Console.WriteLine("An IOException Occurred :" + e.Message); } catch (Exception e) { //Catch any other exception that occurs Console.WriteLine("An Exception Occurred :" + e.Message); } Console.ReadLine(); }
Sanil BhosalePosted Jun 11, 2020, 9:26 AM
Sir, program does not show any output or destination file created or not. Control directly come out after return statement of if(args.Length>2)... Kindly help
Sanil BhosalePosted Jun 11, 2020, 9:24 AM
If (args.Length < 2) { //Show usage information if incorrect number //of parameters has been entered Console.WriteLine("Usage: FileCopy source destination"); return; }