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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With