Answered by:
Finding all file sizes in a directory in c#

Question
-
Hi,
I have a string array with different file names (with path) and I want to find out the size of each one. How can I find the size of each file using C#?
Please suggest!!
Sunday, March 20, 2011 7:50 PM
Answers
-
FileInfo info = new FileInfo(fileNameAndPath);
long size = info.Length;http://msdn.microsoft.com/en-us/library/system.io.fileinfo.length.aspx
http://babaandthepigman.spaces.live.com/blog/- Proposed as answer by Hamid JabarpourfardMVP Monday, March 21, 2011 1:06 PM
- Marked as answer by Cookie Luo Monday, March 28, 2011 2:09 AM
Sunday, March 20, 2011 8:04 PM -
Hello,
you have to use FileInfo class (use name System.IO namespace). You can get info here: http://www.dotnetperls.com/fileinfo-length
Mitja
- Proposed as answer by Hamid JabarpourfardMVP Monday, March 21, 2011 1:06 PM
- Marked as answer by Cookie Luo Monday, March 28, 2011 2:09 AM
Sunday, March 20, 2011 8:10 PM -
Hi Kailashchalwadi,
Another way to achieve the goal is to use byte[] to store the file. The following code shows how to find out the size of the file.
public Form1() { InitializeComponent(); string file = @"d:\test.txt"; byte[] bytes = File.ReadAllBytes(file); label1.Text = GetFileSize(bytes.LongLength); } private string GetFileSize(double byteCount) { string size = "0 Bytes"; if (byteCount >= 1073741824.0) size = String.Format("{0:##.##}", byteCount / 1073741824.0) + " GB"; else if (byteCount >= 1048576.0) size = String.Format("{0:##.##}", byteCount / 1048576.0) + " MB"; else if (byteCount >= 1024.0) size = String.Format("{0:##.##}", byteCount / 1024.0) + " KB"; else if (byteCount >0) size = string.Format("{0:##.##}", byteCount) + " Bytes"; return size; } Hope it helps.
Best Regards,
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Cookie Luo Monday, March 28, 2011 2:09 AM
Wednesday, March 23, 2011 5:17 AM
All replies
-
FileInfo info = new FileInfo(fileNameAndPath);
long size = info.Length;http://msdn.microsoft.com/en-us/library/system.io.fileinfo.length.aspx
http://babaandthepigman.spaces.live.com/blog/- Proposed as answer by Hamid JabarpourfardMVP Monday, March 21, 2011 1:06 PM
- Marked as answer by Cookie Luo Monday, March 28, 2011 2:09 AM
Sunday, March 20, 2011 8:04 PM -
Hello,
you have to use FileInfo class (use name System.IO namespace). You can get info here: http://www.dotnetperls.com/fileinfo-length
Mitja
- Proposed as answer by Hamid JabarpourfardMVP Monday, March 21, 2011 1:06 PM
- Marked as answer by Cookie Luo Monday, March 28, 2011 2:09 AM
Sunday, March 20, 2011 8:10 PM -
Hi Kailashchalwadi,
Another way to achieve the goal is to use byte[] to store the file. The following code shows how to find out the size of the file.
public Form1() { InitializeComponent(); string file = @"d:\test.txt"; byte[] bytes = File.ReadAllBytes(file); label1.Text = GetFileSize(bytes.LongLength); } private string GetFileSize(double byteCount) { string size = "0 Bytes"; if (byteCount >= 1073741824.0) size = String.Format("{0:##.##}", byteCount / 1073741824.0) + " GB"; else if (byteCount >= 1048576.0) size = String.Format("{0:##.##}", byteCount / 1048576.0) + " MB"; else if (byteCount >= 1024.0) size = String.Format("{0:##.##}", byteCount / 1024.0) + " KB"; else if (byteCount >0) size = string.Format("{0:##.##}", byteCount) + " Bytes"; return size; } Hope it helps.
Best Regards,
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Cookie Luo Monday, March 28, 2011 2:09 AM
Wednesday, March 23, 2011 5:17 AM