Reputation: 6524
Is there much of a difference between using the static methods of the File
object as opposed to creating a new FileInfo
object and calling those methods?
Upvotes: 17
Views: 13307
Reputation:
Almost there are no difference. Both delete the file if exists and throw an exception if it doesn't exist.
Upvotes: -1
Reputation: 8393
It depends. If you are performing a single operation use the File
class and if you are performing multiple operations on the same file, use FileInfo
.
EDIT: I made this point as my understanding is that the File class's static methods will always check for the security. But if you are re-using the instance of FileInfo, the methods will do the security check only for the first time and not on each subsequent calls.
Upvotes: 3
Reputation: 41393
The only difference is that File
must resolve the specified path (assuming it is relative), while the FileInfo
should have already have the resolved path.
Upvotes: 16
Reputation: 17701
i hope this will helps you ...
IO.FileInfo
provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of System.IO.FileStream objects
. This class cannot be inherited.
that means, you need to create instance/object of FileInfo to accomplish this processes.
IO.File provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of System.IO.FileStream objects.
that means, you need not to create instance/object of FileInfo to accomplish this processes.
see thess links For more Info
http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx
http://msdn.microsoft.com/en-us/library/system.io.file.aspx
Upvotes: 1