R
rn5a
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?
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?