Example #1
0
        public async Task <TryResult <string> > TryReadTextFileAsync(string path, CancellationToken cancellationToken)
        {
            var contentStringBuilder = new StringBuilder();
            var operationSucceeded   = await TryReadFileCommonAsync(path, async stream =>
            {
                using (var reader = new StreamReader(stream))
                {
                    var buffer = new char[BufferSize];

                    while (reader.Peek() > 0)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return(false);
                        }

                        var charsRead = await reader.ReadAsync(buffer, 0, BufferSize);

                        contentStringBuilder.Append(buffer, 0, charsRead);
                    }
                    return(true);
                };
            }).ConfigureAwait(false);

            return(TryResult.Create(operationSucceeded, operationSucceeded ? contentStringBuilder.ToString() : string.Empty));
        }
        public async Task <TryResult <string> > TryReadTextFileAsync(string path)
        {
            string content            = "";
            bool   operationSucceeded = await TryReadFileCommonAsync(path, async stream => {
                using (var reader = new StreamReader(stream)) {
                    content = await reader.ReadToEndAsync().ConfigureAwait(false);
                    return(true);
                };
            }).ConfigureAwait(false);

            return(TryResult.Create(operationSucceeded, content));
        }
        public async Task <TryResult <byte[]> > TryReadBinaryFileAsync(string path)
        {
            byte[] content            = null;
            bool   operationSucceeded = await TryReadFileCommonAsync(path, async stream => {
                using (var ms = new MemoryStream()) {
                    await stream.CopyToAsync(ms).ConfigureAwait(false);
                    content = ms.ToArray();
                    return(true);
                }
            }).ConfigureAwait(false);

            return(TryResult.Create(operationSucceeded, content));
        }
Example #4
0
        public async Task <TryResult <byte[]> > TryReadBinaryFileAsync(string path, CancellationToken cancellationToken)
        {
            byte[] content            = null;
            var    operationSucceeded = await TryReadFileCommonAsync(path, async stream =>
            {
                using (var ms = new MemoryStream())
                {
                    await stream.CopyToAsync(ms, BufferSize, cancellationToken).ConfigureAwait(false);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(false);
                    }

                    content = ms.ToArray();
                    return(true);
                }
            }).ConfigureAwait(false);

            return(TryResult.Create(operationSucceeded, content));
        }