问题描述
我想使用Azure.Storage.Files.Shares
库读取Azure中存储帐户上FileShare中包含的csv文件的内容。
我能够使用ShareFileClient
连接到文件,但是如何在我的代码中读取内容并进行处理(添加新行)?
ShareFileClient file = ConnectToFile();
Steam content = await file.OpenReadAsync(true);
// gives a Stream object,that I cannot get to work to get the content.
流式传输此文件内容的下一步是什么?我被困在尝试使read操作与类似
一起使用的情况using (Steam stream = new Stream() )
{
// The actions to read the stream go here
}
关于如何实现此目标的任何建议?
解决方法
关于此问题,请参考以下代码
// read
using (var stream = await file.OpenReadAsync().ConfigureAwait(false))
using (var reader = new StreamReader(stream)) {
// read csv file one line by line
while (!reader.EndOfStream) {
var line =await reader.ReadLineAsync().ConfigureAwait(false);
Console.WriteLine(line);
}
}
//write
ShareFileProperties properties = await file.GetPropertiesAsync().ConfigureAwait(false);
var myPosition = properties.ContentLength;
var newData = "42,11,58,\"N\",85,12,45,\"W\",\"Worcester\",ND"+Environment.NewLine;
var bytes = Encoding.UTF8.GetBytes(newData);
await file.SetHttpHeadersAsync(myPosition + bytes.Length);
using (var writer = await file.OpenWriteAsync(overwrite: false,position:(myPosition -1)).ConfigureAwait(false)) {
await writer.WriteAsync(bytes,bytes.Length);
await writer.FlushAsync();
}