none
List Files in a folder

    Question

  • Hello, I am very new to programming and would like some help. I need a program that will search in a supplied folder and list all PDF type of file in a text box, and then have the operator select the file to open. 

    Can someone show me the code to do this?

    Thanks Jac

    Thursday, October 09, 2014 9:40 AM

Answers

  • Here is how I would.

        ''' <summary>
        ''' Returns the path to a pdf file that the user selected. Returns a blank if the user cancelled.
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function OpenPDF() As String
            Dim OFD As New OpenFileDialog
            OFD.Filter = "Pdf Files|*.pdf"
            If OFD.ShowDialog = DialogResult.OK Then
                Return OFD.FileName
            End If
            Return ""
        End Function


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Thursday, October 09, 2014 9:17 PM
    Moderator

All replies

  • Rather than having someone write the code for you, I think you will learn more if you try to write it yourself and ask questions about anything you get stuck with. You can find detailed documentation on the .Net Framework by searching in the Library. Here are some methods that can get you started.

    • Directory.GetFiles will return an array containing all the files in a directory that match a pattern. You will need to add the statement Imports System.IO at the start of your code file.
    • You might want to display the list in a ListBox rather than a TextBox. That will make it easier for the user to select one.
    • Another way of letting the user select a file of a particular type is to use OpenFileDialog.


    Thursday, October 09, 2014 10:32 AM
  • Hello, I am very new to programming and would like some help. I need a program that will search in a supplied folder and list all PDF type of file in a text box, and then have the operator select the file to open. 

    Can someone show me the code to do this?

    Thanks Jac

    A TextBox displays Text. Not different items. Therefore attempting to "select" a file to open from a TextBox, though possible, would be quite difficult.

    A ListBox would be appropriate to the task as each File name could be placed in the ListBox as a separate item. And then the ListBox selected index changed event could be used to open the file.

    However opening a .PDF file requires something to open it in. What control are you trying to open the .PDF file in?


    La vida loca

    Thursday, October 09, 2014 1:10 PM
  • Is there a specific reason why you want to use the textbox? Or better, the listbox? As Blackwood mentions, VB has the open file dialog which will allow the user to start off in a defined folder ("supplied folder") and navigate to where the PDF file is located. The dialog can be configured to display only files of type PDF.

    If all you are looking for is for the user to be able to select a specific PDF file then the open file dialog is the best way to go. Saga


    Insanity is the prelude to discovery

    Thursday, October 09, 2014 5:14 PM
  • Here is how I would.

        ''' <summary>
        ''' Returns the path to a pdf file that the user selected. Returns a blank if the user cancelled.
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function OpenPDF() As String
            Dim OFD As New OpenFileDialog
            OFD.Filter = "Pdf Files|*.pdf"
            If OFD.ShowDialog = DialogResult.OK Then
                Return OFD.FileName
            End If
            Return ""
        End Function


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Thursday, October 09, 2014 9:17 PM
    Moderator