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
In .NET, file iteration lives in the System.IO namespace. For large folders, prefer the streaming API Directory.EnumerateFiles over Directory.GetFiles so you do not load every path into memory at once. You can also choose recursion with SearchOption.AllDirectories. Remember that these APIs return full paths; if you only want the name to display, use Path.GetFileName. Always validate the folder exists and be ready to handle UnauthorizedAccessException and DirectoryNotFoundException when walking the file system.
Example that gathers file names and shows them in a message box, while handling common edge cases:
var folder = @"C:\SomeDirectory";
if (System.IO.Directory.Exists(folder))
{
var sb = new System.Text.StringBuilder();
try
{
foreach (var path in System.IO.Directory.EnumerateFiles(folder, "*", System.IO.SearchOption.TopDirectoryOnly))
sb.AppendLine(System.IO.Path.GetFileName(path));
System.Windows.Forms.MessageBox.Show(sb.ToString());
}
catch (System.UnauthorizedAccessException) { /* inform user or skip */ }
catch (System.IO.DirectoryNotFoundException) { /* inform user */ }
} Docs: Directory.EnumerateFiles (lazy enumeration), SearchOption (control recursion), Directory.Exists (validate paths), and Path.GetFileName (display-friendly names).
Jump to Post— Antenka 274You can use foreach cycle. E.g. like this:
foreach(string fileName f in Directory.GetFiles("SomeDirectory")) { ... }
Jump to Post— LizR 171You 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
Jump to Post— Antenka 274Direcrory class placed in System.IO. So you sould add import to it:
using System.IO;
You can use foreach cycle. E.g. like this:
foreach(string fileName f in Directory.GetFiles("SomeDirectory"))
{
...
} Thanks, but it gives me the error that the Directory doesn't exists in the current context.
How can I declare it?
You can use foreach cycle. E.g. like this:
foreach(string fileName f in Directory.GetFiles("SomeDirectory")) { ... }
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
Direcrory class placed in System.IO. So you sould add import to it:
using System.IO; Thanks, got it. :)
Direcrory class placed in System.IO. So you sould add import to it:
using System.IO;
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.
}
Direcrory class placed in System.IO. So you sould add import to it:
using System.IO;
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?
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
Thanks, I'm done for now. Wasn't using right syntax for message box. Thanks for all your help.
c ya tomorrow!!!
Regards,
Ali
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.