/// <summary> /// Read contents of a file as a byte array. See: hhttps://github.com/FrendsPlatform/Frends.File#ReadBytes /// </summary> /// <returns>Object {byte[] ContentBytes, string Path, double SizeInMegaBytes, DateTime CreationTime, DateTime LastWriteTime } </returns> public static async Task <ReadBytesResult> ReadBytes([PropertyTab] ReadInput input, [PropertyTab] ReadBytesOption options) { return(await ExecuteActionAsync( () => ExecuteReadBytes(input, options), options.UseGivenUserCredentialsForRemoteConnections, options.UserName, options.Password) .ConfigureAwait(false)); }
private static async Task <ReadBytesResult> ExecuteReadBytes(ReadInput input, ReadBytesOption options) { using (var file = new FileStream(input.Path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, useAsync: true)) { var buffer = new byte[file.Length]; await file.ReadAsync(buffer, 0, (int)file.Length).ConfigureAwait(false); return(new ReadBytesResult(new FileInfo(input.Path), buffer)); } }
private static async Task <ReadResult> ExecuteRead(ReadInput input, ReadOption options) { var encoding = GetEncoding(options.FileEncoding, options.EnableBom, options.EncodingInString); using (var fileStream = new FileStream(input.Path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, useAsync: true)) using (var reader = new StreamReader(fileStream, encoding, detectEncodingFromByteOrderMarks: true)) { var content = await reader.ReadToEndAsync().ConfigureAwait(false); return(new ReadResult(new FileInfo(input.Path), content)); } }