Program.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace FileModTest
  6. {
  7. class Program
  8. {
  9. static long position = 0;
  10. static void Main(string[] args)
  11. {
  12. string source = @"D:\SocketClientFile\W.P.S.9999.12012.2019.exe";
  13. string target = source + ".temp";
  14. FileCopy(source, target);
  15. Console.ReadKey();
  16. }
  17. private static void FileCopy(string sourcePath,string targetPath)
  18. {
  19. FileStream fsRead = null;
  20. FileStream fsWrite = null;
  21. try
  22. {
  23. fsRead = new FileStream(sourcePath, FileMode.Open);
  24. byte[] result = new byte[1024 * 1024 * 10];//10 MB缓冲区
  25. fsWrite = new FileStream(targetPath, FileMode.Truncate);
  26. fsWrite.Close();
  27. while (true)
  28. {
  29. int count = fsRead.Read(result, 0, result.Length);
  30. using ( fsWrite = new FileStream(targetPath, FileMode.Append))
  31. {
  32. if (count == 0)
  33. break;
  34. fsWrite.Write(result, 0, count);
  35. position = fsWrite.Position;
  36. Console.WriteLine("已完成 %{0}", (int)((double)fsWrite.Position * 100 / fsRead.Length));
  37. }
  38. }
  39. }
  40. catch (Exception ex)
  41. {
  42. Console.WriteLine(ex.Message.ToString());
  43. }
  44. finally
  45. {
  46. if (fsRead != null)
  47. fsRead.Close();
  48. if (fsWrite != null)
  49. fsWrite.Close();
  50. }
  51. }
  52. //public async void CreateCopyOfFile()
  53. //{
  54. // string dir = @"c:\Mukesh\files\";
  55. // using (StreamReader objStreamReader = File.OpenText(dir + "test.txt"))
  56. // {
  57. // using (StreamWriter objStreamWriter = File.CreateText(dir + "copy_test.txt"))
  58. // {
  59. // await CopyFileToTarget(objStreamReader, objStreamWriter);
  60. // }
  61. // }
  62. //}
  63. //public async Task CopyFileToTarget(StreamReader objStreamReader, StreamWriter objStreamWriter)
  64. //{
  65. // int num;
  66. // char[] buffer = new char[0x1000];
  67. // while ((num = await objStreamReader.ReadAsync(buffer, 0, buffer.Length)) != 0)
  68. // {
  69. // await objStreamWriter.WriteAsync(buffer, 0, num);
  70. // }
  71. //}
  72. }
  73. }