Is it possible to create FileInfo in the file that is currently being written?

In my application (winforms application for C # 4.5) I periodically check the contents of the folder and save information about any files found in the database. As part of this procedure, I create an instance of FileInfo using new FileInfo(path) , and I read the properties CreationTime , LastWriteTime , LastAccessTime , Length and Attributes . I never call any methods in an instance of FileInfo .

What I want to know: is there a risk of errors during a crash, lock, or runtime if the file is currently being written by a third-party application (or while copying to the Windows folder) while I create the FileInfo object or access its properties?

+7
source share
3 answers

Yes, it is "safe." This is considered at a very low level - the file system driver. A file on shared file systems, such as FAT or NTFS, has two different structures on disk. First there is an entry in the directory, it stores metadata about the file. Like name, timestamps, attributes and length. Actual file data is stored elsewhere, a cluster chain in which file data is stored.

FileInfo exclusively provides you with metadata for the file. File data is much more sensitive, very subject to change, as the process writes to the file. It is noteworthy that you can block access to file data using the FileShare options. But there is no way to block metadata. Accordingly, you can always get FileInfo for a file, regardless of what another process is doing with the file.

Of course, the actual properties of FileInfo can be changed when a process is written to a file. They are updated lazily, especially the LastAccessTime property. If you want to be sure that you have accurate information that cannot be changed, you need to get a file lock. Do this by opening the file with FileShare.Read or FileShare.None. This ensures that no other process can open the file for writing if you have a file open. Please note that this can easily raise an IOException, you will only get a lock when another process hasn’t appeared before you and opened the file for writing.

+11
source

No, they are not used in the context that you are using.

From FileSystemInfo.LastWrite -> MSDN

Note. This method may return an inaccurate value because it uses its own functions whose values ​​cannot be constantly updated by the operating system.

But imagine for a second, they return the last values. If you consumed a value (time-dependent) and after that the files are overwritten, your last available values ​​will become incorrect / damaged. So this does not make sense.

+1
source

no problem see msdn:

http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx

Thread Safety Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members do not guarantee thread safety.

and notice:

When properties are first retrieved, FileInfo calls the Refresh method and caches the file information. On subsequent calls, you must call Refresh to get the latest copy of the information.

0
source

All Articles