I\'ve been reading that the static methods of the File
Class are better used to perform small and few tasks on a file like checking to see if it exists and that
The File.Exists
will perform much faster than a new FileInfo(filePath).Exists
- especially over a network and provided the files actually exist. This is because File.Exists
will only check for existence of the file, whereas a new FileInfo(filePath).Exists
first constructs a FileInfo
object, which contains all the properties (dates, size etc) of the file (if it exists).
In my experience with this, even checking for the existence of 10 files over the network is noticeably faster (ie 20ms vs 200ms) by using File.Exists
.
Generally if you are performing a single operation on a file, use the File
class. If you are performing multiple operations on the same file, use FileInfo
.
The reason to do it this way is because of the security checking done when accessing a file. When you create an instance of FileInfo
, the check is only performed once. However, each time you use a static File
method the check is performed.
A FileInfo may be needed to deal with Access Control properties. For the rest it is a Static versus Instance choice and you can pick what is convenient.
The major difference between File
class and FileInfo
class is that
Both members of the
File
andFileInfo
class are decorated with the[System.Security.SecurityCritical] and [System.Security.SecuritySafeCritical]
attribute butFile
class has 'multiple security checks' as compared toFileInfo
class (Read Here) and the check is performed each time when you call a static member of theFile
class.When you create an instance of
FileInfo
, the check is performed only once.
File
is a static type class whereas FileInfo
is an instance type class.FileInfo
class you need to create an instance whereas in File
class you can directly access its members without the need to create an instance.FileInfo
instance methods instead of the corresponding static methods of the File
class.File
class provides more methods as compared to FileInfo
class.Note: Either the SecurityCriticalAttribute attribute or the SecuritySafeCriticalAttribute attribute must be applied to code for the code to perform security-critical operations.
The methods of the
File
andFileInfo
classes are similar, but they differ in that the methods of the File class arestatic
, so you need to pass more parameters than you would for the methods of theFileInfo
instance.You need to do this because it operates on a specific file; for example, the
FileInfo.CopyTo()
method takes one parameter for the destination path that's used to copy the file, whereas theFile.Copy()
method takes two parameters for the source path and the destination path."
References
Yes, and one of the reason could be is, as Nag said Files is a utility class and hence no instance is required to be created. Same time, as File being utility class, each time require security check.
On other hand FileInfo requires instance to be created, and that point it uses security check. Thus, now performing multiple operation using FileInfo will not invoke security checks.