Hi,
Many many thanks, Laurent, for highlighting the differences.
You have said that most of the times, you prefer using the FileInfo
object, creating an instance of it & keeping it for later use when you
need to use it for other purposes but doesn't that mean unnecessarily
consuming more server resources in the meantime?
Yes. I shouldn't have said "most of the time", rather "often". What I
meant is that I often started by using the File method, and suddenly
realized "Oh, I will need that file later for another use" and then
refactoring in order to use a FileInfo instead.
My intent was only to say that though the File class can do a lot, it
cannot replace the FileInfo class completely.
By "in the meantime", I mean that suppose you instantiate the FileInfo
object on the second line of a sub-routine & use it, say, on the fifth
line of the same sub-routine. After that, you do not use this instance,
say, till the 70th line of the sub-routine. This means that all the
code between the 6th & the 69th line gets executed before you use this
instance of the FileInfo object again on the 70th line of the
sub-routine. Why keep the object in the server memory while the code
between the 6th & the 69th line is being executed? Why not just use the
File object twice as & when required?
You know, object oriented design is not saving resources, rather in the
contrary. However, it makes code easier to refactor, modify, maintain,
extend, etc... It also makes it easier to understand. Using static
classes or methods, you can program C# almost "procedurally" (inventing
words here, I think). That doesn't mean that you should do it always.
However, I don't want to push you to use FileInfo all the time (and I
also don't use it always!!), I just wanted to moderate your opinion
which seemed to be partial to the File class and put forward the
advantages of the FileInfo class.
Please correct me if I am wrong.
I don't think you're wrong and I don't think I am right. It really
depends on the scenario ;-)
BTW, apart from the difference which you have talked about, are there
any other differences between File & FileInfo and Directory &
DirectoryInfo?
I think your best bet here is to check the MSDN documentation and
compare which methods each class offers:
http://msdn2.microsoft.com/en-us/library/system.io.fileinfo.aspx
http://msdn2.microsoft.com/en-us/library/system.io.file.aspx
http://msdn2.microsoft.com/en-us/library/system.io.directoryinfo.aspx
http://msdn2.microsoft.com/en-us/library/system.io.directory.aspx
Greetings!
Laurent