Roger Wells wrote:In line 5 I want to have something like "Source Template", "Sample.txt" and only those text files will be highlighted and selectable.
In line 5 you are using FileNameExtensionFilter. "Sample.txt" isn't an extension. Extension is what is after the last . (dot). In your case it is "txt".
Anyway, you can have at most one filename Sample.txt in the same file system directory. Why do you want to filter Sample.txt only? If you want user to select such file, just assume user selected that if that's what you want.
Not quite clear to me actually, what exactly you want. Concrete examples maybe with 3 random filenames?
Roger Wells wrote:The file may be in any directory on the system. There maybe be many text files in the directory. So wherever the file is I want to make sure they get the correct one.
Is there a way to find just Sample.txt ?
The correct one is only one you said - "Sample.txt".
If your goal is to open the file X, and that X you know, and user needs to select that X and nothing else - why do you want to involve that selection process, I don't get that part.
Maybe somebody else understands what you want as I don't - sorry. I don't get maybe because of tired, been hard day.
By only allowing them to see the Sample.txt it will reduce the possibility of error when there can be many other text files in the same directory.
Why would you restrict your users from selecting any file as opposed to a file matching a particular extension ?
Isn' t it possible for your application to actually read the file to validate it's contents ? Your application should be independent of the file name user gives.
What if the user downloads a troll gif image and simply renames it to "sample.txt", will your application accept it ?
And my final question
Have you used any application which only allows you to select 'x' file and not 'y' as per your usecase ?
@OP. Imagine you go through thousand of directories and all of them look empty except one. Why such efforts to put on user?
If you could narrow down the area enough where such templates possibly can be, your program probably would be much more useful if would find all existing templates and would give an ability for user to choose a needed one at a given point in time.
The appended version number at the beginning could be extracted from the Path object.
Show a wizard with the option to select root parent directory as the first screen. This would be the parent directory to search for sample.txt.
When user clicks next, show an indeterminate progress bar (see here) and recursively search the directory and its children for sample.txt.
Once you get all the files, show a table with the file paths and allow the user to select the file(s).