【问题标题】:What are the uses that FileSystemInfo.Refresh() function has? [closed]FileSystemInfo.Refresh() 函数有什么用途? [关闭]
【发布时间】:2013-06-27 05:27:06
【问题描述】:

我想使用函数FileSystemInfo.Refresh()..但是我想知道如果我们调用这个函数会发生什么。

【问题讨论】:

  • 是的,我要做...但是有什么用..它只会像上次修改时间一样刷新属性..或者它有任何其他用途?

标签: c# filesystems


【解决方案1】:

MSDN - FileSystemInfo.Refresh

刷新对象的状态。

调用的原因是为了获取文件的“最新”属性。如果在磁盘上更新了信息,原始对象可能具有陈旧数据。 IE。 MSDN 显式调用了属性大小写:

在尝试获取属性信息之前必须调用 Refresh。

显示陈旧的样本:

// create a file at this location
var fileName = @"E:\Temp\attr.txt";

var fi = new FileInfo(fileName);
Console.WriteLine("Attributes: {0}", fi.Attributes); // Archive
var fi2 = new FileInfo(fileName);
fi2.Attributes = fi2.Attributes | FileAttributes.ReadOnly;
Console.WriteLine("New Attributes: {0}", fi2.Attributes); // Archive, ReadOnly
Console.WriteLine("Stale attributes: {0}", fi.Attributes); // Archive
fi.Refresh();
Console.WriteLine("Refreshed attributes: {0}",fi.Attributes);// Archive, ReadOnly

【讨论】:

  • 谢谢 ..我已经阅读了文档并明白了您所提到的属性...您能给我任何关于对象处于陈旧数据中的示例吗?
  • @kombsh 示例已添加。
  • +1 只是一个例子。
  • 感谢它的工作..
【解决方案2】:

来自MSDN;

FileSystemInfo.Refresh 从当前获取文件的快照 文件系统。

在尝试获取属性之前必须调用 Refresh 信息,否则信息将过时。

它明确使用File.FillAttributeInfo,这是内部方法。

public void Refresh()
{
  this._dataInitialised = File.FillAttributeInfo(this.FullPath, ref this._data, false, false);
}

您可以查看File.​FillAttributeInfo(String, WIN32_FILE_ATTRIBUTE_DATA&, Boolean, Boolean) Method 的工作原理。

来自https://stackoverflow.com/a/1448727/447156

FileInfo 值只加载一次,然后被缓存。要获得 当前值,在获取属性之前调用 Refresh()

你也可以检查这个问题;

【讨论】:

    猜你喜欢
    • 2012-09-02
    • 2011-05-08
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多