473,387 Members | 1,520 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,387 software developers and data experts.

Get File Size Using FileSystemInfo

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 directory, then
the ListItem should display the directory name which will be appended
with the text <DIR>. If an item in the ListBox happens to be a file,
then the ListItem should display the file name which will be appended
with the size of the file. This is how I have done it:

Dim fInfo As FileInfo
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))

For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name))
Else
fInfo = New FileInfo(Server.MapPath("Folder1\MyFile.aspx"))
lstFD.Items.Add(New ListItem(fsi.Name & " - " & fInfo.Length,
fsi.Name))
End If
Next

As such, the above code does get the correct file size (in the Else
condition) but to get the file size, I have to use the FileInfo class
additionally.

Using the FileSystemInfo class, one can get other details of a file
like when was the file created, when was it last written, when was it
last accessed etc. using properties like CreationTime, LastWriteTime,
LastAccessTime etc. respectively but there isn't any FileSystemInfo
property to get the file size (which is why I had to use the FileInfo
class).

Is there any way by which I can get the file size using the
FileSystemInfo class instead of using the FileInfo class?

Jan 8 '07 #1
2 8568
Hi,

rn**@rediffmail.com wrote:
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 directory, then
the ListItem should display the directory name which will be appended
with the text <DIR>. If an item in the ListBox happens to be a file,
then the ListItem should display the file name which will be appended
with the size of the file. This is how I have done it:

Dim fInfo As FileInfo
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))

For Each fsi In dInfo.GetFileSystemInfos
You should not do that. This gets the list of FileSystemInfos on every
loop, which will affect performances. Additionally, if a file gets added
(or worse, deleted) while your program is running, your application may
crash, or at the very least display strange results. You should get the
array of FileSystemInfos first, save it in a local variable, and then
loop using the array.
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name))
Else
fInfo = New FileInfo(Server.MapPath("Folder1\MyFile.aspx"))
lstFD.Items.Add(New ListItem(fsi.Name & " - " & fInfo.Length,
fsi.Name))
End If
Next
The easiest way to recognize if a FileSystemInfo is a FileInfo or a
DirectoryInfo is to use the "Is" keyword. Also, since the object you get
is actually a FileInfo, resp DirectoryInfo (both classes derive from
FileSystemInfo), you can simply cast, you don't need to create a new
instance:

(not totally sure about the VB.NET syntax, but I hope you get the idea)

If ( TypeOf fsi Is DirectoryInfo ) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name))
Else
fInfo = CType( fsi, FileInfo )
lstFD.Items.Add(New ListItem(fsi.Name & " - " & fInfo.Length, fsi.Name))

As such, the above code does get the correct file size (in the Else
condition) but to get the file size, I have to use the FileInfo class
additionally.
Yes, but since (with my corrections) this is the same instance, it's not
a big deal.
Using the FileSystemInfo class, one can get other details of a file
like when was the file created, when was it last written, when was it
last accessed etc. using properties like CreationTime, LastWriteTime,
LastAccessTime etc. respectively but there isn't any FileSystemInfo
property to get the file size (which is why I had to use the FileInfo
class).

Is there any way by which I can get the file size using the
FileSystemInfo class instead of using the FileInfo 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 9 '07 #2
Sorry, Laurent, for getting back to you after such a long time....got
engaged in some other work.
You should get the
array of FileSystemInfos first, save it in a local variable, and then
loop using the array.
Could you please show me an example of how to save the array returned
by GetFileSystemInfos in a local variable & then use the array to loop?

Thanks
Laurent Bugnion [MVP] wrote:
Hi,

rn**@rediffmail.com wrote:
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 directory, then
the ListItem should display the directory name which will be appended
with the text <DIR>. If an item in the ListBox happens to be a file,
then the ListItem should display the file name which will be appended
with the size of the file. This is how I have done it:

Dim fInfo As FileInfo
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))

For Each fsi In dInfo.GetFileSystemInfos

You should not do that. This gets the list of FileSystemInfos on every
loop, which will affect performances. Additionally, if a file gets added
(or worse, deleted) while your program is running, your application may
crash, or at the very least display strange results. You should get the
array of FileSystemInfos first, save it in a local variable, and then
loop using the array.
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name))
Else
fInfo = New FileInfo(Server.MapPath("Folder1\MyFile.aspx"))
lstFD.Items.Add(New ListItem(fsi.Name & " - " & fInfo.Length,
fsi.Name))
End If
Next

The easiest way to recognize if a FileSystemInfo is a FileInfo or a
DirectoryInfo is to use the "Is" keyword. Also, since the object you get
is actually a FileInfo, resp DirectoryInfo (both classes derive from
FileSystemInfo), you can simply cast, you don't need to create a new
instance:

(not totally sure about the VB.NET syntax, but I hope you get the idea)

If ( TypeOf fsi Is DirectoryInfo ) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name))
Else
fInfo = CType( fsi, FileInfo )
lstFD.Items.Add(New ListItem(fsi.Name & " - " & fInfo.Length, fsi.Name))

As such, the above code does get the correct file size (in the Else
condition) but to get the file size, I have to use the FileInfo class
additionally.

Yes, but since (with my corrections) this is the same instance, it's not
a big deal.
Using the FileSystemInfo class, one can get other details of a file
like when was the file created, when was it last written, when was it
last accessed etc. using properties like CreationTime, LastWriteTime,
LastAccessTime etc. respectively but there isn't any FileSystemInfo
property to get the file size (which is why I had to use the FileInfo
class).

Is there any way by which I can get the file size using the
FileSystemInfo class instead of using the FileInfo 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 18 '07 #3

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

Similar topics

1
by: wurlds_wurst_coder | last post by:
I want to create a directory structure and would like the folder attributes set to hidden. How can I do this and will my program be able to insert and delete files when this attribute is set. ...
1
by: Dennis English | last post by:
Hi The following code came be very slow in getting creation time,filename,file length and directoryname for files in a subdirectory because it keeps checking each file sequentially in a slow...
1
by: Kerem Gümrükcü | last post by:
Hi, how can i find out, whether a given string specifies a Path or a file... System.IO.File.Exist(UnknownString) does not work for my application. i exactly need to know if the strng is a path...
5
by: Ram [MSFT] | last post by:
Hi All, I'm trying to programatically (using c#) read the file properties (Title, Summary, Author, Comments etc.... The stuff that shows up on the Summary tab when you see the properties of a...
5
by: JH | last post by:
I am scanning selected drives and computers and capture all files and their attributes. I am using the scripting file system object. It takes a long time to collect the attributes. Is there a...
1
by: David C | last post by:
I have a desktop application which uses AxPdf to view PDF file. axPdf.LoadFile("myFile.PDF"); Without going into much detail, I need to make sure that only one person has a given file in the...
4
by: Stephen | last post by:
Hi all, Suppose I am monitoring a Folder C:\Temp and I drop either a file or directory into this folder How will i programmatically know whether the "item" dropped is a file or a folder ...
3
by: Steph | last post by:
hello, i ve a probleme when deleting a directory and when i want create file immediatly after. 1) Directory.Delete(myPath, true); 2) TextWriter sw = new StreamWriter(myPath +"test.aspx"); i...
1
by: veer | last post by:
hi it looks a silly question but i m getting confused actually i want to copy folders from one location to another like from C:\abc To D:\xyz here abc and xyz are two folders in c and d drive it...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.