locked
Listing file names and displaying .pdf in form RRS feed

  • Question

  • Hi,

    I want to display file names in a list (pdf files) and open them from inside a folder.

    Thanx

    Friday, February 18, 2011 11:33 AM

Answers

  • I'm not sure what controls you specifically talking about here but the parts to retrieve a list of filenames

    and to open then files in there appropriate associated application are here.

    Create a form with a listbox and a button on it.

    Option Strict On
    Option Explicit On
    
    Public Class Form1
    
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim filenames = My.Computer.FileSystem.GetFiles("C:\Windows\Microsoft.NET\Framework\v2.0.50727\en", FileIO.SearchOption.SearchTopLevelOnly, "*.xml")
        ListBox1.Items.AddRange(filenames.ToArray)
      End Sub
    
     
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Process.Start(ListBox1.SelectedItem.ToString)
    
      End Sub
    End Class
    
    

     

    This line can be changes to different folder, to recurse to sub folders as well and to change from XML to whatever extension you want ie. pdf

    Dim filenames = My.Computer.FileSystem.GetFiles("C:\Windows\Microsoft.NET\Framework\v2.0.50727\en", FileIO.SearchOption.SearchTopLevelOnly, "*.xml")
       
    

    The line

         Process.Start(ListBox1.SelectedItem.ToString)

    Will open the selected file from the listbox using the appropriate application for that file type.

     

    • Proposed as answer by Mike FengModerator Wednesday, February 23, 2011 12:22 PM
    • Marked as answer by JMGous Wednesday, February 23, 2011 12:33 PM
    Friday, February 18, 2011 4:46 PM
  • To get filename only, you can use

    System.IO.Path.GetFileName(fullPath)
    

    If don't want extension as well, use this

    System.IO.Path.GetFileNameWithoutExtension(FullPath)
    

    To open the PDF file, you can use

    Process.Start(fullname)
    

    • Proposed as answer by Mike FengModerator Wednesday, February 23, 2011 12:22 PM
    • Marked as answer by JMGous Wednesday, February 23, 2011 12:33 PM
    Monday, February 21, 2011 8:28 PM

All replies

  • I'm not sure what controls you specifically talking about here but the parts to retrieve a list of filenames

    and to open then files in there appropriate associated application are here.

    Create a form with a listbox and a button on it.

    Option Strict On
    Option Explicit On
    
    Public Class Form1
    
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim filenames = My.Computer.FileSystem.GetFiles("C:\Windows\Microsoft.NET\Framework\v2.0.50727\en", FileIO.SearchOption.SearchTopLevelOnly, "*.xml")
        ListBox1.Items.AddRange(filenames.ToArray)
      End Sub
    
     
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Process.Start(ListBox1.SelectedItem.ToString)
    
      End Sub
    End Class
    
    

     

    This line can be changes to different folder, to recurse to sub folders as well and to change from XML to whatever extension you want ie. pdf

    Dim filenames = My.Computer.FileSystem.GetFiles("C:\Windows\Microsoft.NET\Framework\v2.0.50727\en", FileIO.SearchOption.SearchTopLevelOnly, "*.xml")
       
    

    The line

         Process.Start(ListBox1.SelectedItem.ToString)

    Will open the selected file from the listbox using the appropriate application for that file type.

     

    • Proposed as answer by Mike FengModerator Wednesday, February 23, 2011 12:22 PM
    • Marked as answer by JMGous Wednesday, February 23, 2011 12:33 PM
    Friday, February 18, 2011 4:46 PM
  • Hi .

    Is there a way only to display the name and not full path. But that is what I wanted.

    Thanx.

    Monday, February 21, 2011 5:24 AM
  • To get filename only, you can use

    System.IO.Path.GetFileName(fullPath)
    

    If don't want extension as well, use this

    System.IO.Path.GetFileNameWithoutExtension(FullPath)
    

    To open the PDF file, you can use

    Process.Start(fullname)
    

    • Proposed as answer by Mike FengModerator Wednesday, February 23, 2011 12:22 PM
    • Marked as answer by JMGous Wednesday, February 23, 2011 12:33 PM
    Monday, February 21, 2011 8:28 PM