Re: Get File Size Using FileSystemInfo

Tech-Archive recommends: Fix windows errors by optimizing your registry



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
.



Relevant Pages

  • Re: Get File Size Using FileSystemInfo
    ... loop using the array. ... Dim fInfo As FileInfo ... Dim fsi As FileSystemInfo ... DirectoryInfo is to use the "Is" keyword. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: [MSH] File oriented cmdlets need to accept FileSystemInfo or FileInfo objects
    ... taking FileSystemInfo, FileInfo, and DirectoryInfo. ... actually takes MshPaths not file system paths, ... We call this a delay-bind ScriptBlock. ...
    (microsoft.public.windows.server.scripting)
  • Re: Correctly identifying FileInfo vs. DirectoryInfo entries
    ... FileSystemInfo list and set properties accordingly (IsFile, IsDir, ... FileSystemInfo entry is a FileInfo or a DirectoryInfo type. ... FileAttributes.Directory) test but throws an Illegal Cast exception ... call a Refresh() method). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem creating FileInfo Array
    ... FileInfo[] fi; ... DirectoryInfo[] di = dirInfo.GetDirectories; ... > I've the following file system: ... > my desire is to create an array of fileinfo containing the a.jpg, ...
    (microsoft.public.dotnet.csharp.general)
  • Re: File & FileInfo
    ... Many many thanks, Laurent, for highlighting the differences. ... I mean that suppose you instantiate the FileInfo ... object on the second line of a sub-routine & use it, say, on the fifth ... and DirectoryInfo. ...
    (microsoft.public.dotnet.framework.aspnet)