如何从 Azure 文件共享下载文件末尾

How do I download the end of a file from azure file share

此代码总是returns一个空字符串

        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% 只需要阅读最后一行(它是一个文本文件)。这里是否缺少某些东西或我可以用来实现此目的的其他 azure 方法?

您在使用 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();
        }
    }
} 

我的测试文件:

以及测试结果: