473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

File & FileInfo

To work with files, when should one use the File class & when should
one use the FileInfo class?

Similarly, to work with directories, when should one use the Directory
class & when should one use the DirectoryInfo class?

Jan 6 '07 #1
3 2152
Hi,

rn**@rediffmail.com wrote:
To work with files, when should one use the File class & when should
one use the FileInfo class?
The File class is static, you cannot instantiate a File, only a
FileInfo. So if you need to use methods without having an instance to
worry about, the File class is good. However, most of the time, I prefer
to use FileInfo, create an instance and keep it for later, when I need
to use it for other purposes. That's exactly the same for Directory
(static) and DirectoryInfo.
Similarly, to work with directories, when should one use the Directory
class & when should one use the DirectoryInfo class?
HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 6 '07 #2
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?

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?

Please correct me if I am wrong.

BTW, apart from the difference which you have talked about, are there
any other differences between File & FileInfo and Directory &
DirectoryInfo?
Laurent Bugnion [MVP] wrote:
Hi,

rn**@rediffmail.com wrote:
To work with files, when should one use the File class & when should
one use the FileInfo class?

The File class is static, you cannot instantiate a File, only a
FileInfo. So if you need to use methods without having an instance to
worry about, the File class is good. However, most of the time, I prefer
to use FileInfo, create an instance and keep it for later, when I need
to use it for other purposes. That's exactly the same for Directory
(static) and DirectoryInfo.
Similarly, to work with directories, when should one use the Directory
class & when should one use the DirectoryInfo class?

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 6 '07 #3
Hi,

rn**@rediffmail.com wrote:
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/lib....fileinfo.aspx
http://msdn2.microsoft.com/en-us/lib...m.io.file.aspx
http://msdn2.microsoft.com/en-us/lib...ctoryinfo.aspx
http://msdn2.microsoft.com/en-us/lib...directory.aspx

Greetings!
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt dittman | last post by:
I have created a windows service that reads emails from a drop directory and moves them to the appropriate mail folder every 15 seconds. I can move, rename and delete the files as needed, up...
2
by: Suresh Gladstone | last post by:
Hi, This is a bit with versioning and installation of the .NET dlls. I want to perform the following, 1. A third party application will be invoking my .NET dll through COM interop . For this I...
0
by: how can i insert a image to SQLServer? | last post by:
I want to insert some strings and images which from richTextBox into SQLServer. I could insert it into SQLServer,such as follows: FileStream s=new FileStream...
5
by: IcingDeath via DotNetMonster.com | last post by:
I am building this SQL Server database app in which i can store files. In order to display files I want to have the app show the associated icon for the extension of the file that is in the...
1
by: Tim Failes | last post by:
This seems a trival question, but I cannot get it to work properly... Essentially my question is, how can I create a text file, and guarantee it is given the current date/time as the Creation Time?...
6
by: Andrew Matthews | last post by:
Hi All, I have the following little class of iterators that allow me to iterate over elements in the file system. I have nested some of them, and then added Func<FileInfo, booldelegates to filter...
3
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file (file.move) to the same name plus todays date. ...
2
by: rn5a | last post by:
Using the FileSystemInfo class, I am retrieving all the directories & files existing in a particular directory on the server & listing them in a ListBox. If an item in the ListBox happens to be a...
5
by: djhexx | last post by:
Hello. I have an ASP.NET application (C#) that I allow users to upload files. Files get stored in a SQL2005 database. The file data is stored in a varbinary(max) column. When the user uploads...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.