Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i'm new to C# this is the code i program select path and see all file name in ricttextbox
when i test i select Folder1 (Folder1 have 40 files) my program works perfectly then i select Folder2 (folder2 have 8,318 Files, 307 Folders) then my program not responding stoped working
Why is that?
Is another way to do that?
@path is selectedpath from folderBrowserDialog
C# , windows form application
i'm new to c#
THANK IN ADVANCE !
C#
string[] filePaths = Directory.GetFiles(@path, "*", SearchOption.AllDirectories);
            foreach (var item in filePaths)
            {
                var r = item.Substring(item.LastIndexOf(@"\") + 1);
                richTextBox1.Text = richTextBox1.Text + r + Environment.NewLine;
            }
Posted
Updated 22-Jul-13 1:32am
v2
Comments
Pheonyx 22-Jul-13 7:34am    
I imagine it is still working, it is just processing a lot and takes time.
How long have you left it running for?
Manoj Chamikara 22-Jul-13 8:35am    
i give 5min and move my application's form side of window then application not responding
Thank you for your response
[no name] 22-Jul-13 7:34am    
You either need to look into threading or realize that somethings just take time.
Herman<T>.Instance 22-Jul-13 7:35am    
Thats it. Threading, Events and Delegates
OriginalGriff 22-Jul-13 7:55am    
Nah - all he's got to do is stop allocating 8318 new strings...a string builder will probably fix 99% of the problem!

1 solution

That is always going to be slow. Why? Because you are concatenating strings 8318 times, and each time generates a whole new string large enough to hold the whole new string, and then dumping it into a RichTextbox which will do more processing each time.

First improvement: use a StringBuilder instead: it is a lot more efficient as it doesn't need to be created anew each time, and then set the RichTextbox content just once, at the end of the loop.
That reduced the time to 23 milliseconds on mine, for 33,000 files...

If that isn't fast enough there are ways to improve it further, but I suspect that will be enough.
 
Share this answer
 
Comments
ridoy 22-Jul-13 8:05am    
agree..+5
Manoj Chamikara 22-Jul-13 8:31am    
Hay dude Thanks for the quick response i modified my code like this it works.
Thank you so much check my code is it you say?

StringBuilder allfiles = new StringBuilder();
string[] filePaths = Directory.GetFiles(@path, "*", SearchOption.AllDirectories);
foreach (var item in filePaths)
{
var r = item.Substring(item.LastIndexOf(@"\") + 1);

allfiles.Append(r).Append(Environment.NewLine);

}
richTextBox1.Text = allfiles.ToString();
OriginalGriff 22-Jul-13 9:29am    
There is a small improvement:
allfiles.AppendLine(r);
instead of
allfiles.Append(r).Append(Environment.NewLine);
Manoj Chamikara 22-Jul-13 9:48am    
dude in this
allfiles.AppendLine(r);
output in richtextbox :1.jpg2.jpe3.wma4.mp4
that's why i use
allfiles.Append(r).Append(Environment.NewLine); then output in richtextbox
1.jpg
2.jpe
3.wma
4.mp4
OriginalGriff 22-Jul-13 10:57am    
Not in my RichTextBox it doesn't! :laugh:
That is what appendline does - it adds an Environment.Newline automatically. Works fine on my system...

BTW: You can also replace
var r = item.Substring(item.LastIndexOf(@"\") + 1);
With
string r = Path.GetFileName(item);
which is a little clearer to read.

[edit]Replaced "s" with "item" - OriginalGriff[/edit]

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