Silentbob
Silentbob

Reputation: 3065

Using System.IO.directory.getfiles

I am using the following code to get a list of pdf's and place them in an array, I am then searching them using autocomplete extender. Everything works fine but System.IO.directory.getfiles always returns the path of the file which I dont want.

Any ideas.

    Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
    'Create array of movies   
    Dim files() As String = System.IO.Directory.GetFiles("c:\pdfs")

    ' Return matching movies   
    Return (
         From m In files
         Where m.Contains(prefixText)
         Select m).Take(count).ToArray()
End Function

Upvotes: 1

Views: 3316

Answers (2)

Steve
Steve

Reputation: 216291

Try with this LINQ to remove the path from the fullfilename list returned

Dim files = Directory.GetFiles("c:\pdfs", "*.pdf").Select(Function(s) Path.GetFileName(s))

Please, also note that Directory.GetFiles could retrieve just the PDF if you use the right overload

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460118

So you want the filename instead of the full path?! Use the Path class:

Dim files() As String = System.IO.Directory.GetFiles("c:\pdfs")
Dim names = From path in files
            Let fileName = IO.Path.GetFileName(path)
            Where fileName.StartsWith(prefixText)
            Select fileName  Take count

Note that i've used StartsWith instead of Contains since the variablename prefixText suggests that this is more appropriate.

Upvotes: 0

Related Questions