[英]How do I download the end of a file from azure file share
此代码始终返回一个空字符串
CloudFile cFile = fShare.getFile(subDir, rootDir, logFileName, AzureConstants.PATH);
if (cFile.Exists())
{
using (var ms = new MemoryStream())
{
long?offset =Convert.ToInt64(cFile.Properties.Length * .8);
long? length = Convert.ToInt64(cFile.Properties.Length * .20);
cFile.DownloadRangeToStream(ms, offset, length);
using (var sr = new StreamReader(ms))
{
return sr.ReadToEnd();// this does run and it returns an empty string ""
}
}
}
我正在尝试读取文件的最后20%,而不是先下载整个文件,然后再阅读最后20%。 甚至不需要最后20%的内容,只需阅读最后一行(它是一个文本文件)即可。 这里是否缺少某些东西或其他一些我可以用来实现此目的的天蓝色方法?
在使用StreamReader之前,您忘记将内存流的位置设置为零。
示例代码如下:
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.File;
using System;
using System.IO;
namespace ConsoleApp19
{
class Program
{
static void Main(string[] args)
{
string s1 = "";
CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("your account", "your key"), true);
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare share = fileClient.GetShareReference("t11");
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
CloudFile file =rootDir.GetFileReference("test.txt");
if (file.Exists())
{
using (var ms = new MemoryStream())
{
long? offset = Convert.ToInt64(file.Properties.Length * .8);
long? length = Convert.ToInt64(file.Properties.Length * .20);
file.DownloadRangeToStream(ms, offset, length);
//set the position of memory stream to zero
ms.Position = 0;
using (var sr = new StreamReader(ms))
{
s1 = sr.ReadToEnd();
}
Console.WriteLine(s1);
}
}
Console.WriteLine("---done---");
Console.ReadLine();
}
}
}
我的测试文件:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.