在方法中从 Azure FileShare 读取文本文件
read text file from Azure FileShare in a method
我想使用 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();
}
我想使用 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();
}