943,703 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 15123
  • C# RSS
Jan 4th, 2009
0

Loop through all files in folder

Expand Post »
Hi,

This is my second post in continuation to my previous one. How can I loop through all the files in a folder.

Cheers!
Ali
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 4th, 2009
0

Re: Loop through all files in folder

You can use foreach cycle. E.g. like this:
c# Syntax (Toggle Plain Text)
  1. foreach(string fileName f in Directory.GetFiles("SomeDirectory"))
  2. {
  3. ...
  4. }
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Jan 4th, 2009
0

Re: Loop through all files in folder

Thanks, but it gives me the error that the Directory doesn't exists in the current context.

How can I declare it?

Click to Expand / Collapse  Quote originally posted by Antenka ...
You can use foreach cycle. E.g. like this:
c# Syntax (Toggle Plain Text)
  1. foreach(string fileName f in Directory.GetFiles("SomeDirectory"))
  2. {
  3. ...
  4. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 4th, 2009
0

Re: Loop through all files in folder

You dont.. Its a predefined class in .net - have a check in your helpfile, its clever and tells you what references (if necessary) and what "using" statements you would need
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 4th, 2009
0

Re: Loop through all files in folder

Direcrory class placed in System.IO. So you sould add import to it:
c# Syntax (Toggle Plain Text)
  1. using System.IO;
Last edited by Antenka; Jan 4th, 2009 at 4:12 pm.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Jan 4th, 2009
0

Re: Loop through all files in folder

Thanks, got it.

Click to Expand / Collapse  Quote originally posted by Antenka ...
Direcrory class placed in System.IO. So you sould add import to it:
c# Syntax (Toggle Plain Text)
  1. using System.IO;
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 4th, 2009
0

Re: Loop through all files in folder

How can I display a message showing the fileNames retrieved from the given directory.

1.
foreach(string fileName f in Directory.GetFiles("SomeDirectory"))
2.
{
3.
...
4.
}

Click to Expand / Collapse  Quote originally posted by Antenka ...
Direcrory class placed in System.IO. So you sould add import to it:
c# Syntax (Toggle Plain Text)
  1. using System.IO;
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 4th, 2009
0

Re: Loop through all files in folder

Have you looked at what you get in the foreach loop?
OK while that code wont work specifically it has a big raving clue in it...

What do you think the clue is?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 4th, 2009
0

Re: Loop through all files in folder

When you call GetFiles method, it return to you an array of strings (filenames). To show them in the message, you should organize them in way, that you like into a string. After that you can see here a bit about showing messages: MessageBox Class
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Jan 4th, 2009
0

Re: Loop through all files in folder

Thanks, I'm done for now. Wasn't using right syntax for message box. Thanks for all your help.

c ya tomorrow!!!
Regards,
Ali

Click to Expand / Collapse  Quote originally posted by Antenka ...
When you call GetFiles method, it return to you an array of strings (filenames). To show them in the message, you should organize them in way, that you like into a string. After that you can see here a bit about showing messages: MessageBox Class
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: TagPrefix and Assembly error
Next Thread in C# Forum Timeline: spss to c#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC