本文整理汇总了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;
}
}
示例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;
}
}
示例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];
}
示例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);
}
示例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);
}
}