What is the difference between File and FileInfo in C#?

C#File

C# Problem Overview


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 we should use an instance of the FileInfo Class if we are going to perform many operations on a specific file.

I understand this and can simply use it that way blindly, but I would like to know why is there a difference?

What is it about the way they work that make them suitable for different situations? What is the point of having this two different classes that seem do the same in different ways?

It would be helpful if someone could answer at least one of this questions.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

> The methods of the File and FileInfo classes are similar, but they differ in that the methods of the File class are static, so you need to pass more parameters than you would for the methods of the FileInfo 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 the File.Copy() method takes two parameters for the source path and the destination path."

References

Solution 3 - C#

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.

Solution 4 - C#

File is optimized for one-off operations on a file, FileInfo is optimized around multiple operations on the same file, but in general there isn't that much difference between the different method implementations.

If you want to compare the exact implementations, Use Reflector to look at both classes.

Solution 5 - C#

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.

Solution 6 - C#

FileInfo:

  • Need to instantiate before using
  • Contains instance methods
  • Cache Info about the File and you need to call Refresh every time to get the latest info about the File

File:

  • No need to instantiate
  • Contains static methods
  • Do not cache, so you get latest info every time you use it.

src:

Solution 7 - C#

FileInfo is an instance of a file thus representing the file itself. File is a utility class so can work with any file

Solution 8 - C#

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.

Solution 9 - C#

Recently I faced problem with File.Exist, I hate this function. After than I've used Fileinfo class Exist function then my program works correct.

Actually what happen in development enviornment File.Exist works well but when it goes to live environment this function is blocking the file object due to that reason I am getting the error access denied and not able to use the file.

This is my learning. I will never used File.Exist method best is to create the object and then use it. Be aware to use static methods.

Solution 10 - C#

The major difference between File class and FileInfo class is that

> Both members of the File and FileInfo class are decorated with the [System.Security.SecurityCritical] and [System.Security.SecuritySafeCritical] attribute but File class has 'multiple security checks' as compared to FileInfo class (Read Here) and the check is performed each time when you call a static member of the File class. > > When you create an instance of FileInfo, the check is performed only once.

  • Apart from these, other minor differences are that File is a static type class whereas FileInfo is an instance type class.
  • Therefore to access the members of 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.
  • If you are performing multiple operations on the same file, it can be more efficient to use FileInfo instance methods instead of the corresponding static methods of the File class.
  • However, 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbluediapenteView Question on Stackoverflow
Solution 1 - C#John RaschView Answer on Stackoverflow
Solution 2 - C#madcolorView Answer on Stackoverflow
Solution 3 - C#Nick WallaceView Answer on Stackoverflow
Solution 4 - C#Franci PenovView Answer on Stackoverflow
Solution 5 - C#Henk HoltermanView Answer on Stackoverflow
Solution 6 - C#DragneelFPSView Answer on Stackoverflow
Solution 7 - C#NagView Answer on Stackoverflow
Solution 8 - C#divView Answer on Stackoverflow
Solution 9 - C#brijeshView Answer on Stackoverflow
Solution 10 - C#Tahir77667View Answer on Stackoverflow