ASP.NET GetFileName to return one file only using wildcard

Hello,

I am looking into return a single file name using wildcard as I have prefix in the file name. How can I do this?

Sameple files (xxxxx_ is the prefix)
12345_dsafdsfadf.gif
23223_asfdsafdsadfas.jpg

Thanks in advance.
LVL 1
webcertifiedAsked:
Who is Participating?
 
drypzConnect With a Mentor Commented:
Hi! You may try this one
    private List<string> GetFileNames(string searchPattern, SearchOption searchOption, string directory)
    {
        List<string> fileNames = new List<string>();

        DirectoryInfo di = new DirectoryInfo(directory);

        foreach (FileInfo fi in di.GetFiles(searchPattern, searchOption))
        {
            fileNames.Add(fi.Name);
        }

        return fileNames;
    }

Usage:

List<string> fileNames = GetFileNames("?????_.jpg", SearchOption.AllDirectories, @"C:\");//you may use question mark(?) or asterisk(*) to represent your wildcard character

Open in new window

0
 
webcertifiedAuthor Commented:
This does not return single file string which I am looking for.
Thanks.
0
 
CodeCruiserConnect With a Mentor Commented:
Try this code

Dim Files() As String = Directory.GetFiles("path", "12345_*.*")

If Files.Count = 1
   'Textbox1.Text = Files(0)
End If
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.