Search directory

Posted on 2011-05-06
Last Modified: 2013-11-11
How to query filename in a directory and return 10 filenames at a time?
1-10 file
a user click next, provide the next 10 filename
Question by:VBdotnet2005
    LVL 53

    Accepted Solution

    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

    LVL 16

    Assisted Solution

    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.
    LVL 53

    Expert Comment

    @CuteBug: I agree with your remark, but it was just an example to show the user of Skip() and Take() in linq

    Author Comment

    Dhaest & CuteBug,

    I have never used cache before. Is it like session, or collection? How can I use it in this case?

    Author Comment

    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

    Author Comment

    I am not sure what ByRef page As Integer is for. Could you explain a bit?

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now