1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- namespace FileModTest
- {
- class Program
- {
- static long position = 0;
- static void Main(string[] args)
- {
- string source = @"D:\SocketClientFile\W.P.S.9999.12012.2019.exe";
- string target = source + ".temp";
- FileCopy(source, target);
- Console.ReadKey();
- }
- private static void FileCopy(string sourcePath,string targetPath)
- {
- FileStream fsRead = null;
- FileStream fsWrite = null;
- try
- {
- fsRead = new FileStream(sourcePath, FileMode.Open);
- byte[] result = new byte[1024 * 1024 * 10];//10 MB缓冲区
- fsWrite = new FileStream(targetPath, FileMode.Truncate);
- fsWrite.Close();
- while (true)
- {
- int count = fsRead.Read(result, 0, result.Length);
- using ( fsWrite = new FileStream(targetPath, FileMode.Append))
- {
- if (count == 0)
- break;
- fsWrite.Write(result, 0, count);
- position = fsWrite.Position;
- Console.WriteLine("已完成 %{0}", (int)((double)fsWrite.Position * 100 / fsRead.Length));
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message.ToString());
- }
- finally
- {
- if (fsRead != null)
- fsRead.Close();
- if (fsWrite != null)
- fsWrite.Close();
- }
- }
- //public async void CreateCopyOfFile()
- //{
- // string dir = @"c:\Mukesh\files\";
- // using (StreamReader objStreamReader = File.OpenText(dir + "test.txt"))
- // {
- // using (StreamWriter objStreamWriter = File.CreateText(dir + "copy_test.txt"))
- // {
- // await CopyFileToTarget(objStreamReader, objStreamWriter);
- // }
- // }
- //}
- //public async Task CopyFileToTarget(StreamReader objStreamReader, StreamWriter objStreamWriter)
- //{
- // int num;
- // char[] buffer = new char[0x1000];
- // while ((num = await objStreamReader.ReadAsync(buffer, 0, buffer.Length)) != 0)
- // {
- // await objStreamWriter.WriteAsync(buffer, 0, num);
- // }
- //}
- }
- }
|