I've come across some strange behavior trying to get files that start with a certain string.
Please would someone give a working example on this:
I want to get all files in a directory that begin with a certain string, but also contain the xml extension.
for example:
apples_01.xml
apples_02.xml
pears_03.xml
I want to be able to get the files that begin with apples.
So far I have this code
DirectoryInfo taskDirectory = new DirectoryInfo(this.taskDirectoryPath);
FileInfo[] taskFiles = taskDirectory.GetFiles("*.xml");
GetFiles(String, String, SearchOption) Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.
Public Function GetFiles As FileInfo()Returns a file list from the current directory. For complete list of properties and methods please visit Microsoft's documentation.
To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null.
FileInfo[] taskFiles = taskDirectory.GetFiles("apples*.xml");
var taskFiles = taskDirectory.GetFiles("*.xml").Where(p => p.Name.StartsWith("apples"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With