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?
7 Answers
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.

- 40,753
- 6
- 122
- 148
-
You're meaning that given the path is relative, the work must be done immediately before `File.Delete()` calls the Win32 Delete method. Whereas with `FileInfo`, the path would have already been resolved to an absolute path upon creation. Is this a correct interpretation of what you're saying? In other words, this is work the CLR does behind the scenes that could potentially impact performance given `File.Delete()` is called often enough with the same relative path. – Tim Coker Oct 14 '11 at 20:32
-
@TimCoker - That's correct. If you already have a FileInfo, then you could save a call to `Path.GetFullPathInternal`, which does a bit of work to normalize/resolve the path. As @errorstacks points out, one is a static method and one is an instance. So you can't call FileInfo.Delete without an instance of FileInfo. Assuming you have that, then the only difference is the path resolution. – CodeNaked Oct 14 '11 at 23:03
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.

- 8,223
- 6
- 49
- 81
-
3When down voting it sure would be refreshing to know why..This is a Q/A site and also a learning resource..if the answer is totally out to lunch give some info... – Jesse Oct 14 '11 at 20:26
-
I'm curious why you got downvoted, too. You made an interesting point there. – Tim Coker Oct 14 '11 at 20:28
-
1I didn't downvote, but I probably would have. You've provided a very vague rationale for your recommendation. Back it up with references, if you feel it's correct. – Michael Petrotta Oct 14 '11 at 20:29
-
Perhaps because it was a little off from your initial question? When calling delete there is no difference....I just wanted to point out that when performing multiple operations it would be better to use FileInfo... – Jesse Oct 14 '11 at 20:31
-
2@Jesse - Sorry, I did downvote, but got pulled away. Your statement about the security is incorrect and you can use Reflector to verify. Both Delete methods will demand the exact same permissions on the file. – CodeNaked Oct 14 '11 at 23:06
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

- 17,397
- 27
- 100
- 203
Almost there are no difference. Both delete the file if exists and throw an exception if it doesn't exist.
-
1That is not true (at least no longer true) FileInfo.Delete does nothing if the file does not exist, see http://msdn.microsoft.com/en-us/library/system.io.fileinfo.delete(v=vs.110).aspx – Robba Mar 21 '14 at 16:02