Tim Coker
Tim Coker

Reputation: 6524

File.Delete() versus FileInfo.Delete()

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

Answers (7)

user586399
user586399

Reputation:

Almost there are no difference. Both delete the file if exists and throw an exception if it doesn't exist.

Upvotes: -1

Jesse
Jesse

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

Ilia G
Ilia G

Reputation: 10211

Both are calling Win32Native.DeleteFile()

Upvotes: 7

CodingGorilla
CodingGorilla

Reputation: 19842

Nope, they're basically the same thing.

Upvotes: 1

CodeNaked
CodeNaked

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

Glory Raj
Glory Raj

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

James L
James L

Reputation: 16864

No, they are duplicated for convenience

Upvotes: 0

Related Questions