-2

I would like to use the function FileSystemInfo.Refresh()..but I want to know what will happen if we call this function.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Kevin M
  • 5,436
  • 4
  • 44
  • 46
  • Yes I am going to do...but what is the use.. it will only refresh the attribute like last modified time..or it has any other use? – Kevin M Jun 27 '13 at 05:34

2 Answers2

5

MSDN - FileSystemInfo.Refresh

Refreshes the state of the object.

The reason to call is to get "latest" properties of the file. The original object may have stale data if information was updated on disk. I.e. MSDN explicitly calls out attribute case:

Calls must be made to Refresh before attempting to get the attribute information.

Sample showing staleness:

// 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
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

From MSDN;

FileSystemInfo.Refresh takes a snapshot of the file from the current file system.

Calls must be made to Refresh before attempting to get the attribute information, or the information will be outdated.

It is explicitly uses File.FillAttributeInfo which is internal method.

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

You can check how File.​FillAttributeInfo(String, WIN32_FILE_ATTRIBUTE_DATA&, Boolean, Boolean) Method works.

From https://stackoverflow.com/a/1448727/447156

The FileInfo values are only loaded once and then cached. To get the current value, call Refresh() before getting a property

You can check this question also;

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364