Search directory

How to query filename in a directory and return 10 filenames at a time?
sample
1-10 file
a user click next, provide the next 10 filename
VBdotnet2005Asked:
Who is Participating?
 
DhaestConnect With a Mentor Commented:
Easy example below

        Dim test As IEnumerable(Of String)

        test = getFiles(1, "c:\windows")

        test = getFiles(3, "c:\windows")
Private Function getFiles(ByRef page As Integer, ByRef directory As String) As IEnumerable(Of String)
        Dim Files = (From file In My.Computer.FileSystem.GetFiles(directory)
                    Order By file _
                    Select file).Skip((page - 1) * 10).Take(10)

        Return Files
    End Function

Open in new window

0
 
CuteBugConnect With a Mentor Commented:
Hi VBDotNet2005,
         @Dhaest: Your function will call FileSystem.GetFiles each time the getFiles method is called. This might be CPU intensive.

          What you need to do is call the My,Computer.FileSystem.GetFiles() once and cache the result in you class.

          Then each time the next is pressed, use the skip and take methods to get the appropriate data.
0
 
DhaestCommented:
@CuteBug: I agree with your remark, but it was just an example to show the user of Skip() and Take() in linq
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
VBdotnet2005Author Commented:
Dhaest & CuteBug,

I have never used cache before. Is it like session, or collection? How can I use it in this case?
0
 
VBdotnet2005Author Commented:
Also, I am trying to convert C#.  Why error below?  
public IEnumerable<string> getFiles(ref int page, ref string directory)
        {
            var Files = (from file in directory
                         orderby file
                         select file).Skip((1 - 1) * 10).Take(1);

            return Files;  <<<<<   error
        }
0
 
VBdotnet2005Author Commented:
I am not sure what ByRef page As Integer is for. Could you explain a bit?
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.