Zebra0.com

csharp files

The Directory command has several optional parameters.

If we would like to select just the files with the extension png (Portable Network Graphic) add the pattern:

string[] files = Directory.GetFiles(@"c:\MyData\","*.png");

Notice that the pattern is not case sensitive. It return both png and PNG files.

If the directory contains sub folders, they are not returned.
This will only return the top level files.
If we want to return the names of all files, including in subdirectories, we can specify that.

 string[] files = Directory.GetFiles(@"c:\MyData\", "*.png", SearchOption.AllDirectories);

When you run the program you will see a list of all the files png, including some in subdirectories.

Note: You can't leave out the second argument and include the search option, or use just a comma. Use "*" for the pattern:

string[] files = Directory.GetFiles(@"c:\MyData\", "*", SearchOption.AllDirectories);

End of lesson, Next lesson: