Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I'm new to C#
I set the folder path using folderBrowserDialog and i want to know how to get it's folder name and size. i also need sub folder's names , size.
and all file's names and size in sub folders
C# / Windows form application

THANK IN ADVANCE!
Posted
Comments
Joezer BH 21-Jul-13 5:51am    
What seems to be the problem? Have you tried anything?
Show some code if you can
Manoj Chamikara 21-Jul-13 6:05am    
var di = new DirectoryInfo(path);
var allFiles = di.GetFiles("*", SearchOption.AllDirectories);
foreach (var file in allFiles)
{

richTextBox1.Text = file.Name + " " + file.Length;
}

path is select path from folderBrowserDialog1
i use this code for that but only i get one file name and it's size only :(
Joezer BH 21-Jul-13 7:00am    
See notice in my updated solution below ...
ridoy 21-Jul-13 5:52am    
not a good question
Manoj Chamikara 21-Jul-13 6:05am    
sorry about my poor English

Hello Manoj,

Try this video for help on basic actions with the FolderBrowserDialog:
C# Beginners Tutorial - 56 - FolderBrowserDialog[^]



[update]

Notice that in the code sample you posted you keep updating the same richTextBox1.Text over and over again, so obviously you will only see the last file's information!
C#
foreach (var file in allFiles)
{
    richTextBox1.Text = file.Name + " " + file.Length;
}
[/update]



Good luck,
Edo
 
Share this answer
 
v2
Comments
ridoy 21-Jul-13 5:58am    
good link..+5 :)
Joezer BH 21-Jul-13 5:59am    
Tnx
Manoj Chamikara 21-Jul-13 6:37am    
Thank you very much Maimonides that's very useful
Joezer BH 21-Jul-13 6:46am    
You're welcome, glad I could help out
Manoj Chamikara 21-Jul-13 8:37am    
Thank You dude i update my code you Notice now it's fine
foreach (var file in allFiles)
{
richTextBox1.Text = richTextBox1.Text+file.Name + " " + File.Length+Environment.NewLine;
}
No one here don't give exact codes of what you want.You need to try yourself and come here when you get stuck.Have a start from here which will help you a lot..
Example for FolderBrowserDialog in C#[^]
http://onemancoder.wordpress.com/2012/01/27/a-better-folderbrowserdialog-in-c/[^]
http://www.c-sharpcorner.com/uploadfile/mahesh/folderbrowserdialog-in-C-Sharp/[^]
 
Share this answer
 
Comments
Manoj Chamikara 21-Jul-13 6:48am    
of course i'm stuck. that's why i'm here. don't misunderstand me. i didn't Show code that's my fault your links are very useful Thanks a lot ridoy
ridoy 21-Jul-13 6:56am    
don't misunderstand you man,if you show your code here then it will be much easier to help you in that specific problem area.Btw,i am glad that i can do some help of you :)
Manoj Chamikara 21-Jul-13 8:29am    
i know dude. if i show my code you can help me easier that's my fault sorry about that. by the way your links are grade thanks again
There is no simple single command to do that - but it isn't that difficult. The only complication is avoiding processing system files and folders when you iterate through folders (if you do, an exception will be thrown):
C#
private bool IsIgnorable( string dir )
    {
    if (dir.EndsWith( ":System Volume Information" )) return true;
    if (dir.Contains( ":$RECYCLE.BIN" )) return true;
    return false;
    }

Other than that, it's just a case for recursing through folders, getting the files and folders list, and processing them using the FileInfo class:
C#
foreach (string s in Directory.GetFiles( dir ))
    {
    FileInfo fi = new FileInfo( s );
    size += fi.Length;
    }
 
Share this answer
 
Comments
Manoj Chamikara 21-Jul-13 6:49am    
Thank you OriginalGriff You always help my questions
OriginalGriff 21-Jul-13 7:10am    
You're welcome!

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