Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

file

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.

like image 445
bluediapente Avatar asked Aug 24 '09 21:08

bluediapente


1 Answers

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.

like image 132
John Rasch Avatar answered Sep 27 '22 21:09

John Rasch