Re: Get File Size Using FileSystemInfo
- From: "Laurent Bugnion [MVP]" <galasoft-lb@xxxxxxxxxx>
- Date: Tue, 09 Jan 2007 08:45:32 +0100
Hi,
rn5a@xxxxxxxxxxxxxx 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
.
- Follow-Ups:
- Re: Get File Size Using FileSystemInfo
- From: rn5a
- Re: Get File Size Using FileSystemInfo
- References:
- Get File Size Using FileSystemInfo
- From: rn5a
- Get File Size Using FileSystemInfo
- Prev by Date: Re: Problem with menu control and CreateChildControls
- Next by Date: Re: Viewstate Without Serialization
- Previous by thread: Get File Size Using FileSystemInfo
- Next by thread: Re: Get File Size Using FileSystemInfo
- Index(es):
Relevant Pages
|