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


C# FileStream.ReadAsync方法代码示例

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


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

示例1: AnonymousPipeReadViaFileStream

        public async Task AnonymousPipeReadViaFileStream(bool asyncReads)
        {
            using (var server = new AnonymousPipeServerStream(PipeDirection.Out))
            {
                Task serverTask = server.WriteAsync(new byte[] { 0, 1, 2, 3, 4, 5 }, 0, 6);

                using (var client = new FileStream(new SafeFileHandle(server.ClientSafePipeHandle.DangerousGetHandle(), false), FileAccess.Read, bufferSize: 3))
                {
                    var arr = new byte[1];
                    for (int i = 0; i < 6; i++)
                    {
                        Assert.Equal(1, asyncReads ?
                            await client.ReadAsync(arr, 0, 1) :
                            client.Read(arr, 0, 1));
                        Assert.Equal(i, arr[0]);
                    }
                }

                await serverTask;
            }
        }
开发者ID:Corillian,项目名称:corefx,代码行数:21,代码来源:Pipes.cs

示例2: NamedPipeReadViaAsyncFileStream

        public async Task NamedPipeReadViaAsyncFileStream(bool asyncReads)
        {
            string name = Guid.NewGuid().ToString("N");
            using (var server = new NamedPipeServerStream(name, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
            {
                Task serverTask = Task.Run(async () =>
                {
                    await server.WaitForConnectionAsync();
                    await server.WriteAsync(new byte[] { 0, 1, 2, 3, 4, 5 }, 0, 6);
                });

                WaitNamedPipeW(@"\\.\pipe\" + name, -1);
                using (SafeFileHandle clientHandle = CreateFileW(@"\\.\pipe\" + name, GENERIC_READ, FileShare.None, IntPtr.Zero, FileMode.Open, (int)PipeOptions.Asynchronous, IntPtr.Zero))
                using (var client = new FileStream(clientHandle, FileAccess.Read, bufferSize: 3, isAsync: true))
                {
                    var arr = new byte[1];
                    for (int i = 0; i < 6; i++)
                    {
                        Assert.Equal(1, asyncReads ?
                            await client.ReadAsync(arr, 0, 1) :
                            client.Read(arr, 0, 1));
                        Assert.Equal(i, arr[0]);
                    }
                }

                await serverTask;
            }
        }
开发者ID:Corillian,项目名称:corefx,代码行数:28,代码来源:Pipes.cs

示例3: ReadByteAsync

 private static async Task<byte> ReadByteAsync(FileStream fs)
 {
     byte[] oneByte = new byte[1];
     Assert.Equal(1, await fs.ReadAsync(oneByte, 0, 1));
     return oneByte[0];
 }
开发者ID:SamuelEnglard,项目名称:corefx,代码行数:6,代码来源:WriteAsync.cs

示例4: ReadAsyncCore

 private static async Task ReadAsyncCore(FileStream reader, int innerIterations, byte[] bytes)
 {
     for (int i = 0; i < innerIterations; i++)
         await reader.ReadAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
 }
开发者ID:ChuangYang,项目名称:corefx,代码行数:5,代码来源:Perf.FileStream.cs

示例5: SimpleWriteAsync

        public async Task SimpleWriteAsync()
        {
            using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
            {
                await fs.WriteAsync(TestBuffer, 0, TestBuffer.Length);
                Assert.Equal(TestBuffer.Length, fs.Length);
                Assert.Equal(TestBuffer.Length, fs.Position);

                fs.Position = 0;
                byte[] buffer = new byte[TestBuffer.Length];
                Assert.Equal(TestBuffer.Length, await fs.ReadAsync(buffer, 0, buffer.Length));
                Assert.Equal(TestBuffer, buffer);
            }
        }
开发者ID:SamuelEnglard,项目名称:corefx,代码行数:14,代码来源:WriteAsync.cs


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