Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("测试***************");
            CreateFileConfig config = new CreateFileConfig()
            {
                IsAsync = true, CreateUrl = "D:\\Desktop\\A.txt"
            };
            CopyFileConfig copyFileConfig = new CopyFileConfig(true, "D:\\Desktop\\A.txt", "D:\\Desktop\\B.txt");

            UpFileSingleTest.Create(config);
            UpFileSingleTest.Copy(copyFileConfig);
            UpFileSingleTest.CopyBigFile("E:\\电影\\捉丶妖丶记 HD1080P.mp4", "D:\\Desktop\\2.mp4", 10, 1024 * 100);
            Console.WriteLine("完成");
            Console.Read();
        }
Exemplo n.º 2
0
 /// <summary>
 /// 根据配置需求,复制文件
 /// </summary>
 /// <param name="config"></param>
 public static void Copy(IFileConfig config)
 {
     lock (_lockObj)
     {
         CopyFileConfig copyFileConfig = config as CopyFileConfig;
         if (copyFileConfig == null)
         {
             return;
         }
         if (copyFileConfig.OriginalFileUrl == null || copyFileConfig.DestinationFileUrl == null)
         {
             return;
         }
         if (!File.Exists(copyFileConfig.OriginalFileUrl))
         {
             return;
         }
         FileStream stream = copyFileConfig.IsAsync ?
                             new FileStream(copyFileConfig.OriginalFileUrl, FileMode.Open, FileAccess.ReadWrite, FileShare.Read,
                                            4096, true)
             : new FileStream(copyFileConfig.OriginalFileUrl, FileMode.Open, FileAccess.ReadWrite, FileShare.Read,
                              4096, false);
         byte[] originalFileBytes = new byte[stream.Length];
         using (stream)
         {
             if (stream.CanRead)
             {
                 if (stream.IsAsync)
                 {
                     // 异步
                     copyFileConfig.OriginalFileStream = stream;
                     copyFileConfig.OriginalFileBytes  = originalFileBytes;
                     stream.BeginRead(originalFileBytes, 0, originalFileBytes.Length, EndReadFileCallBack, copyFileConfig);
                 }
                 else
                 {
                     // 同步
                     stream.Read(originalFileBytes, 0, originalFileBytes.Length);
                     using (FileStream copyStream = new FileStream(copyFileConfig.DestinationFileUrl, FileMode.Create))
                     {
                         copyStream.Write(originalFileBytes, 0, originalFileBytes.Length);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 3
0
        private static void EndReadFileCallBack(IAsyncResult ar)
        {
            CopyFileConfig config = ar.AsyncState as CopyFileConfig;

            if (config == null)
            {
                return;
            }
            config.OriginalFileStream.EndRead(ar);
            if (File.Exists(config.DestinationFileUrl))
            {
                File.Delete(config.DestinationFileUrl);
            }
            FileStream copyFileStream = new FileStream(config.DestinationFileUrl, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 4096, true);

            using (copyFileStream)
            {
                Console.WriteLine("异步复制原文件地址:{0}", config.OriginalFileStream.Name);
                Console.WriteLine("复制后的新文件地址:{0}", config.DestinationFileUrl);
                copyFileStream.BeginWrite(config.OriginalFileBytes, 0, config.OriginalFileBytes.Length, EndCreateFileCallBack, copyFileStream);
            }
        }