When doing something like ... = myFileInfo.FullName – does it access the drive or is it loaded to memory with the creation of myFileInfo?
When doing something like … = myFileInfo.FullName – does it access the drive or
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, they are saved in memory – it’s a snapshot of the file when you created the FileInfo class and first accessed a property. You can call the Refresh method to make sure the properties are up to date.
You can also use the static members of the File class to read the file info at that point in time, however, this incurs the overhead of a security check each time.
My evidence for this comes from using ILSpy; we can see that, if we access a property like Length:
Then, if the internal data has not been initialized, Refresh is called:
This then populates the data for subsequent checks. So unless you call Refresh again, you’ll get the data from the point in time you first accessed a property.
The reason for caching this data is inferred from the FileInfo MSDN article:
It obviously caches this data to avoid the expense of a SecurityCheck each time, leaving it up to the user to call Refresh and decide when to pay this cost.