Click here to Skip to main content
15,901,368 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How do I Open files of the same extension one by one using a for loop

I want to display the content in them in a message box one by one using a for loop.

Thanks in Advance
Posted
Updated 24-Apr-11 2:37am
v2
Comments
Ed Nutting 24-Apr-11 8:38am    
Edited to remove shouting - boldness and big font is a put off, someone will only see it if they have already clicked to take an interest in your question and so it is more likely to put them off than to encourage them.

If would be easy, but there is one problem with "files of the same extension" and Directory.GetFiles — it does not work as one can expect.
You will find the explanation and the resolution here:
Directory.Get.Files search pattern problem[^].

For reading the file, use System.IO.StreamReader, in particular, System.IO.StreamReader.ReadToEnd.

—SA
 
Share this answer
 
Comments
Ed Nutting 24-Apr-11 15:02pm    
Good answer, my 5.
Sergey Alexandrovich Kryukov 24-Apr-11 21:06pm    
Thank you very much,
--SA
This is a relatively simple answer and I believe we've had this one before, a simple search would get you the answer:

http://msdn.microsoft.com/en-us/library/bb513869.aspx[^]

This can easily be modified with a simple if block that checks the FileInfo Extension property.

Hope this helps, try searching next time,

Ed :)

Edit: A few VB solutions I found as well :
http://stackoverflow.com/questions/4607912/visual-basic-iterate-a-folders-files[^]
http://support.microsoft.com/kb/306666[^]
http://www.dreamincode.net/forums/topic/118490-looping-through-files/[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 24-Apr-11 13:25pm    
This is not as simple as it looks -- Directory.GetFiles does not work as it should -- a know problem which needs a workaround. My 4.
Please see explanation and resolution in my Solution.
--SA
Ed Nutting 24-Apr-11 15:01pm    
Interesting, I have never noticed that problem before though a simple workaround would surely be just to test for exact extension match on the filenames returned?
Sergey Alexandrovich Kryukov 24-Apr-11 21:11pm    
If would be; but the codelet you can see in my answer provides the same interface as you would have with GetFiles, with a fix. I used to do a mistake of not taking this problem into account; Abhishek Sur is the one credited for pointing out this problem.
--SA

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