当前位置: 首页>>代码示例>>C#>>正文


C# FileStream.CopyToAsync方法代码示例

本文整理汇总了C#中System.IO.FileStream.CopyToAsync方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.CopyToAsync方法的具体用法?C# FileStream.CopyToAsync怎么用?C# FileStream.CopyToAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.FileStream的用法示例。


在下文中一共展示了FileStream.CopyToAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Perform

 public async Task Perform()
 {
     using (var sourceStream = new FileStream(_oldPath, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, FileOptions.Asynchronous | FileOptions.DeleteOnClose))
     {
         using (var destinationStream = new FileStream(_newPath, FileMode.Create, FileAccess.Write, FileShare.None, DefaultBufferSize, FileOptions.Asynchronous))
         {
             await sourceStream.CopyToAsync(destinationStream);
         }
     }
 }
开发者ID:RamanBut-Husaim,项目名称:.NET-Practice,代码行数:10,代码来源:RenameOperation.cs

示例2: ReadAllBytesAsync

		/// <summary>
		/// Read all bytes from a specified file asynchronously.
		/// </summary>
		/// <param name="filePath">File path</param>
		/// <param name="bufferSize">Buffer size</param>
		/// <param name="cancellationToken">CancellationToken</param>
		/// <returns>Byte array of file</returns>
		public static async Task<byte[]> ReadAllBytesAsync(string filePath, int bufferSize, CancellationToken cancellationToken)
		{
			using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
			using (var ms = new MemoryStream())
			{
				await fs.CopyToAsync(ms, bufferSize, cancellationToken).ConfigureAwait(false);
				return ms.ToArray();
			}
		}
开发者ID:nagyist,项目名称:WinDesktop-FlashAir-SnowyImageCopy,代码行数:16,代码来源:FileAddition.cs

示例3: TranscodeInternal

		public async Task TranscodeInternal(CancellationToken ct, IWaveStreamProvider stream, Stream targetStream)
		{
			var tempFilename = Path.Combine(Path.GetTempPath(), GetTranscodedFileName(Guid.NewGuid().ToString() + ".extension"));			
			MediaFoundationEncoder.EncodeToMp3(stream, tempFilename, 320000);			
			using (var tempStream = new FileStream(tempFilename, FileMode.Open, FileAccess.Read))
			{
				await tempStream.CopyToAsync(targetStream, 81920, ct);
			}
			File.Delete(tempFilename);
		}
开发者ID:Galad,项目名称:musicmirror,代码行数:10,代码来源:IWaveStreamTranscoder.cs

示例4: CopyTo

            public async Task CopyTo(string destination) {
                if (!File.Exists(_filename)) {
                    throw new FileNotFoundException(ToolsStrings.DirectoryInstallator_FileNotFound, _filename);
                }

                using (var input = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
                using (var output = new FileStream(destination, FileMode.Create)) {
                    await input.CopyToAsync(output);
                }
            }
开发者ID:gro-ove,项目名称:actools,代码行数:10,代码来源:DirectoryContentInstallator.cs

示例5: ExecuteAsync

        /// <summary>
        /// Executes the result against the provided service context asynchronously.
        /// </summary>
        /// <param name="context">The service context.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A task executing the result.</returns>
        public virtual async Task ExecuteAsync(IServiceContext context, CancellationToken cancellationToken)
        {
            context.Response.Output.Buffer = false;
            context.Response.Output.Clear();

            string etag = context.Response.GenerateEtag(FilePath);

            if (etag == context.Request.Headers.TryGet(IfNoneMatchHeaderName))
            {
                context.Response.SetStatus(HttpStatusCode.NotModified);
                return;
            }

            try
            {
                using (var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    context.Response.SetHeader(context.Response.HeaderNames.ContentLength, fileStream.Length.ToString(CultureInfo.InvariantCulture));
                    context.Response.SetHeader(context.Response.HeaderNames.ContentType, ContentType ?? DefaultHtmlContentType);
                    context.Response.SetHeader(context.Response.HeaderNames.ETag, etag);
                    context.Response.SetCharsetEncoding(context.Request.Headers.AcceptCharsetEncoding);

                    await fileStream.CopyToAsync(context.Response.Output.Stream, Convert.ToInt32(fileStream.Length), cancellationToken);
                }
            }
            catch (ArgumentException)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError, Global.InvalidFilePathOrUrl);
            }
            catch (FileNotFoundException)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError, Global.InvalidFilePathOrUrl);
            }
            catch (DirectoryNotFoundException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound, Global.InvalidFilePathOrUrl);
            }
            catch (PathTooLongException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound, Global.InvalidFilePathOrUrl);
            }
            catch (IOException)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError, Global.InaccessibleFile);
            }
            catch (SecurityException)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden, Global.InaccessibleFile);
            }
            catch (UnauthorizedAccessException)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden, Global.InaccessibleFile);
            }
        }
开发者ID:dstarosta,项目名称:GitProjects,代码行数:60,代码来源:HtmlFileResult.cs

示例6: LoadToBuffer

 public static async Task<byte[]> LoadToBuffer(string FileName) {
     byte[] buffer = null;
     var f = Application.Environment.ApplicationBasePath + "/" + FileName;
     using (var fs = new FileStream(f, FileMode.Open, FileAccess.Read)) {
         using (var ms = new MemoryStream()) {
             await fs.CopyToAsync(ms);
             buffer = ms.ToArray();
         }
     }
     return buffer;
 }
开发者ID:matthewhancock,项目名称:portsdems,代码行数:11,代码来源:File.cs

示例7: BackupROM

 private async Task BackupROM()
 {
     // Copy the original file here with a .bak extension
     using (FileStream original = new FileStream(romPath, FileMode.Open, FileAccess.Read, FileShare.None, 4096, true))
     {
         using (FileStream backup = new FileStream(romPath + ".bak", FileMode.Create, FileAccess.Write, FileShare.None, 4096, true))
         {
             await original.CopyToAsync(backup);
         }
     }
 }
开发者ID:craigshaw,项目名称:MegaROM,代码行数:11,代码来源:BinROMLoader.cs

示例8: Configure

 public void Configure(IApplicationBuilder app)
 {
     app.Run(async (ctx) =>
     {
         var filePath = Path.Combine(_env.ApplicationBasePath, "Testfile");
         using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 1024 * 64,
             FileOptions.Asynchronous | FileOptions.SequentialScan))
         {
             await fileStream.CopyToAsync(ctx.Response.Body);
         }
     });
 }
开发者ID:MJomaa,项目名称:KestrelResponseStreamBug,代码行数:12,代码来源:Startup.cs

示例9: UploadAsync

 public async Task UploadAsync(string url, IDictionary<string, string> headers, string method, string file)
 {
     byte[] data = null;
     using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, true))
     {
         using (var memStream = new MemoryStream())
         {
             await fileStream.CopyToAsync(memStream);
             data = memStream.ToArray();
         }
     }
     await UploadAsync(url, headers, method, data);
 }
开发者ID:neilunadkat,项目名称:appacitive-dotnet-sdk,代码行数:13,代码来源:WebClientHttpFileHandler.cs

示例10: ParseFilePath

        async Task<Stream> IMessageDataRepository.Get(Uri address, CancellationToken cancellationToken)
        {
            string filePath = ParseFilePath(address);

            string fullPath = Path.Combine(_dataDirectory.FullName, filePath);
            if (!File.Exists(fullPath))
                throw new FileNotFoundException("The file was not found", fullPath);

            using (var stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, FileOptions.Asynchronous))
            {
                var memoryStream = new MemoryStream();
                await stream.CopyToAsync(memoryStream, DefaultBufferSize, cancellationToken);
                
                memoryStream.Position = 0;
                return memoryStream;
            }
        }
开发者ID:nicklv,项目名称:MassTransit,代码行数:17,代码来源:FileSystemMessageDataRepository.cs

示例11: RetrieveData

 public async Task<MemoryStream> RetrieveData(Guid sourceId, string currentFileName)
 {
     if (!File.Exists(currentFileName)) return (MemoryStream) Stream.Null;
     using (var sourceStream =
         new FileStream(
             currentFileName,
             FileMode.Open,
             FileAccess.Read,
             FileShare.Read,
             4096,
             true))
     {
         var destination = new MemoryStream();
         await sourceStream.CopyToAsync(destination);
         return destination;
     }
 }
开发者ID:Warrenn,项目名称:Carbon.MVC,代码行数:17,代码来源:StreamManager.cs

示例12: GetUncompressedImage

        public async Task<HttpResponseMessage> GetUncompressedImage()
        {
            using (var ms = new MemoryStream())
            {
                var baseDir = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent;
                var filePath = baseDir.FullName + "/Content/Images/app-icon-1024.png";

                using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    fs.Position = 0;
                    await fs.CopyToAsync(ms);
                }

                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(ms.ToArray())
                };
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");

                return result;
            }
        }
开发者ID:SneakyMax,项目名称:Microsoft.AspNet.WebApi.MessageHandlers.Compression,代码行数:22,代码来源:FileController.cs

示例13: Invoke

 public override async Task Invoke(IOwinContext context)
 {
     RequestContentType requestContentType = GetContentType(context.Request.Path.Value);
     if(requestContentType != null)
     {
         context.Response.ContentType = requestContentType.ContentType;
         string filePath = MapPath(context.Request.Path.Value);
         if (File.Exists(filePath))
         {
             using (var contentStream = new FileStream(filePath, FileMode.Open))
             {
                 await contentStream.CopyToAsync(context.Response.Body);
             }
         }
         else
         {
             await Next.Invoke(context);
         }
     }
     else
     {
         await Next.Invoke(context);
     }
 }
开发者ID:fhrn71,项目名称:SnappetChallenge,代码行数:24,代码来源:HttpGetResourceComponent.cs

示例14: Invoke

        public async Task Invoke(IDictionary<string, object> env)
        {
            var req = new OwinRequest(env);
            string file;
            if (!_router.TryGet(req.Path, out file))
            {
                await _next(env);
            }

            var resp = new OwinResponse(req)
            {
                ContentType = "text/html"
            };

            if (!File.Exists(file))
            {
                resp.StatusCode = 404;
                await resp.WriteAsync("File not found");
            }
            using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                await fs.CopyToAsync(resp.Body);
            }
        }
开发者ID:riberk,项目名称:Riberk.Owin.StaticContent,代码行数:24,代码来源:StaticContentModule.cs

示例15: Invoke

        public async Task Invoke(HttpContext httpContext)
        {
            string currentUrl = httpContext.Request.GetDisplayUrl();

            string filePath = _routingService.GetPathForUrl(currentUrl);

            if (filePath != null)
            {
                string fileExtension = GetFileExtension(filePath);

                httpContext.Response.ContentType = _mimeTypeResolver[fileExtension];

                httpContext.Response.StatusCode = 200;

                using (var fileStream = new FileStream(filePath, FileMode.Open))
                {
                    await fileStream.CopyToAsync(httpContext.Response.Body);
                }
            }
            else
            {
                httpContext.Response.StatusCode = 404;
            }
        }
开发者ID:bilal-fazlani,项目名称:StupidWebServer,代码行数:24,代码来源:StupidFileMiddleware.cs


注:本文中的System.IO.FileStream.CopyToAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。