Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to count files in directory but only files that larger then 200kb

i have this but only count all files

C#
private void button1_Click(object sender, EventArgs e)
       {

 int fileCount = Directory.GetFiles(@"C:").Length;

 label1.Text = fileCount.ToString();
 }
    
    }
}
Posted
Updated 14-Jun-15 12:14pm
v2
Comments
[no name] 14-Jun-15 20:35pm    
Have you tried Google? https://www.google.com.au/?gws_rd=ssl#q=get+size+file+c%23

Hi,

DirectoryInfo.GetFiles() will return you FileInfo[] which is list of all files in given folder path. To get length (size) of file you have to use FileInfo.Length property which returns you length in bytes.

Here in your case you have to divide file length/1024 to get size in Kb.

Try with below code -
DirectoryInfo dir = new DirectoryInfo("folder path here");
//divide Length/1024 to convert it in kb.
var files = dir.GetFiles().Where(m => (m.Length / 1024) > 200);


Now, files will hold list of files from your given folder having size greater than 200Kb.

Hope this helps.
 
Share this answer
 
Comments
[no name] 14-Jun-15 22:46pm    
Also further to get count of file you can use files.Count()
Member 11383935 15-Jun-15 3:25am    
No error but in label1 ( label1.Text = files.ToString(); ) i get this
System.Linq.Enumerable+WhereArrayIterator`1[System.IO.FileInfo]
what im doing wrong?

[no name] 15-Jun-15 7:05am    
Here ffiles is IEnumerable object, a list.

To get only count use files.count(), or to accesscontents you have to iterate it as a List
F-ES Sitecore 15-Jun-15 8:12am    
What do you expect to see when you use ToString on a List of complex items? That .net is going to magically know what you want to see? If you use ToString on a class that does not implement a specific ToString implementation then you simply get the type name back which is what you're seeing. You need to loop through each file and get the data from it you need, ie the filename, the whole path, whatever, and show that data in the label.
if you need only count of files, not the actual list use below code as -
C#
int fileCount = dir.GetFiles().Where(m => (m.Length / 1024) > 200).Count();
 
Share this answer
 
did you mean something like this

private void button1_Click(object sender, EventArgs e)
{ int fileCount = Directory.GetFiles(@"C:").Where(m => (m.Length / 1024) > 200).Count();
label1.Text = fileCount.ToString();
 
Share this answer
 
Comments
[no name] 15-Jun-15 14:33pm    
Yes, if you only need file count to assign to label1.
Member 11383935 15-Jun-15 15:10pm    
i thinks that something is wrong with mathematics
it always return null
[no name] 15-Jun-15 15:39pm    
Well the code you put here will not work. Directory.GetFiles will return you string[] means list of file names not actual file objects.

If you want to access file object to get length here you have to use DirectoryInfo.GetFiles which return you list of files. Check with solution 2 I posted it still works for me.


DirectoryInfo dir = new DirectoryInfo("folder path here");
int fileCount = dir.GetFiles().Where(m => (m.Length / 1024) > 200).Count();
label1.Text = fileCount.ToString();
Member 11383935 15-Jun-15 16:01pm    
this work perfectly, thanks
[no name] 15-Jun-15 16:02pm    
ok

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900