在方法中从Azure FileShare读取文本文件



我想使用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.

流式传输此文件内容的下一步是什么?我一直在努力让读取操作与这样的东西一起工作

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, 0, bytes.Length);
await writer.FlushAsync();
}

最新更新