473,569 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(S erver.MapPath(" Folder1"))

For Each fsi In dInfo.GetFileSy stemInfos
If ((fsi.Attribute s And FileAttributes. Directory) = 16) Then
lstFD.Items.Add (New ListItem(fsi.Na me & " <DIR>", fsi.Name))
Else
fInfo = New FileInfo(Server .MapPath("Folde r1\MyFile.aspx" ))
lstFD.Items.Add (New ListItem(fsi.Na me & " - " & 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 8595
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(S erver.MapPath(" Folder1"))

For Each fsi In dInfo.GetFileSy stemInfos
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.Attribute s And FileAttributes. Directory) = 16) Then
lstFD.Items.Add (New ListItem(fsi.Na me & " <DIR>", fsi.Name))
Else
fInfo = New FileInfo(Server .MapPath("Folde r1\MyFile.aspx" ))
lstFD.Items.Add (New ListItem(fsi.Na me & " - " & 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.Na me & " <DIR>", fsi.Name))
Else
fInfo = CType( fsi, FileInfo )
lstFD.Items.Add (New ListItem(fsi.Na me & " - " & 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 GetFileSystemIn fos 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(S erver.MapPath(" Folder1"))

For Each fsi In dInfo.GetFileSy stemInfos

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.Attribute s And FileAttributes. Directory) = 16) Then
lstFD.Items.Add (New ListItem(fsi.Na me & " <DIR>", fsi.Name))
Else
fInfo = New FileInfo(Server .MapPath("Folde r1\MyFile.aspx" ))
lstFD.Items.Add (New ListItem(fsi.Na me & " - " & 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.Na me & " <DIR>", fsi.Name))
Else
fInfo = CType( fsi, FileInfo )
lstFD.Items.Add (New ListItem(fsi.Na me & " - " & 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
2363
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. TIA introspectre ATSIGN earthlink PERIOD net
1
1782
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 DSL connection Does anybody know how I can speed things? One suggestion was to get the remote server to only bring back creation...
1
1524
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 or a file... But how the h*** can i find it out....
5
9840
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 file). FileInfo and FileSystemInfo classes expose only the standard properties (create time, mod time etc..). Any pointers would be greatly...
5
1581
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 better and faster way of capturing file attributes of all files in a selected drive or computer. CreateObject("Scripting.FileSystemObject") Folder =...
1
1612
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 PDF viewer. So before calling LoadFile, I need to make sure that nobody else is reading the file. Played around FileSystemInfo to see if there is...
4
1324
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 Thanks, stephen
3
2059
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 obtain Exception Details: System.UnauthorizedAccessException: Access to the path '***' is denied.
1
1355
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 works fine when whole folder including sub folders of "abc" are to be copied into "xyz" folder but i have a index file "index.xls" which contain some...
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.