【问题标题】:How do we track azure file storage folder is updated or not?我们如何跟踪 azure 文件存储文件夹是否更新?
【发布时间】:2018-08-27 08:09:55
【问题描述】:

我有 azure 文件存储,其中包含其他文件夹。 我想查看或跟踪该文件夹的最后更新时间?

我已经做了一个简单的控制台应用程序来从该位置获取所有文件 -

        // Get list of all files/directories on the file share 
        CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["storageConnectionString"]);
        CloudFileClient fileClient = cloudStorageAccount.CreateCloudFileClient();
        CloudFileShare fileShare = fileClient.GetShareReference(ConfigurationManager.AppSettings["shareName"]);

        var sourceName = fileShare.GetRootDirectoryReference().GetDirectoryReference((ConfigurationManager.AppSettings["sourceName"]));
        IEnumerable<IListFileItem> fileList = sourceName.ListFilesAndDirectories();

        CloudFileDirectory destinationDir = fileShare.GetRootDirectoryReference().GetDirectoryReference((ConfigurationManager.AppSettings["destinationName"]));

        foreach (IListFileItem listItem in fileList)
        {
         // gives me all file records 
        }

我试图得到它,但它是nullvar test = sourceName.Properties.LastModified;

因为 null 我不能使用这个查询:(

 var latest= fileShare.GetRootDirectoryReference().ListFilesAndDirectories()
    .OfType<CloudFileShare>()
    .OrderByDescending(m => m.Properties.LastModified)
    .ToList()
    .First();

我刚刚注意到,我刚刚将一个新文件上传到该文件夹​​,但 LastModified 仍然是旧日期,这意味着 LastModified 对于该文件夹和文件是分开的:O 现在该怎么办?

我想检查是否有任何新文件在 24 小时内更新到主文件夹的任何子文件夹中。

我想知道如何从该源文件夹中检查最后更新的文件日期时间?

为什么 LastModified 为空?

【问题讨论】:

    标签: c# azure azure-storage file-storage


    【解决方案1】:

    对于retrieve property values,在您的 blob 或容器上调用 FetchAttributesAsync 方法以填充属性,然后读取值。

    当您运行控制台应用程序时,它实际上会创建一个 CloudFileShare 对象的新实例,并且其属性会初始化为默认值。您需要为此调用FetchAttributes 方法来填充属性。

    此外,当您列出文件的属性时,文件的属性也会被提取,您不需要创建CloudFileShare 的新实例。你可以参考这个thread

    您可以在检索属性之前使用 sourceName.FetchAttributes();。我测试了它,它工作正常。请参考以下代码:

    CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["storageConnectionString"]);
    CloudFileClient fileClient = cloudStorageAccount.CreateCloudFileClient();
    CloudFileShare fileShare = fileClient.GetShareReference(ConfigurationManager.AppSettings["shareName"]);
    var sourceName = fileShare.GetRootDirectoryReference().GetDirectoryReference((ConfigurationManager.AppSettings["sourceName"]));
    sourceName.FetchAttributes();
    var test = sourceName.Properties.LastModified;
    

    【讨论】:

    • 非常感谢,这只是给我LastModified for sourcename 我想知道 sourcename 是否有任何最近更新的子文件夹,那么 sourcename 的整体 LastModified 应该是它的子文件夹值.例如 - sourcename 在 8 月 11 日有文件 LastModified,suourcename 内的子文件夹在 16aug 被修改,然后整个主文件夹值是 16aug
    • 是的,这将是最新修改的数据。
    • 哦,我刚刚注意到,我刚刚将一个新文件上传到该文件夹​​,但 LastModified 仍然是旧日期,这意味着 LastModified 对于该文件夹和文件是分开的:O 现在该怎么办?
    • 您可以使用DateTime dt = File.GetLastWriteTime(path); 来获取修改数据。并遍历文件夹中的所有文件。这是一个类似的case
    • 你可以把first改成FirstOrDefault试试看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多