none
How to open Files that was loaded into a Listbox without extentions. RRS feed

  • Question

  • How do I go aboute opening an  Files that was loaded into a Listbox without extentions.  Below is the code I use to load the File Names. 

    For Each file As String In Files
                If Not Path.GetFileName(file).Contains("TEMPLATE") Then
                    RecipeListBox.Items.Add(Path.GetFileNameWithoutExtension(file))
                End If
            Next
    This is the code I am using to try open the Files, it gives me an error "can not find the file spesifide"
     Private Sub RecipeListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles RecipeListBox.SelectedIndexChanged
    
            Process.Start(IO.Path.Combine(RecipeFolder, Me.RecipeListBox.SelectedItem.ToString))
    
        End Sub

    Sunday, March 1, 2020 11:13 AM

Answers

  • I have atemted this, since I like simple not complecated. 

    Is ther no way of doing it like this?

     RecipeFolder = RecipeFolder & ".docx"
            Dim Files() As String = Directory.GetFiles(RecipeFolder)
    
            For Each file As String In Files
                If Not Path.GetFileName(file).Contains("TEMPLATE") Then
                    RecipeListBox.Items.Add(Path.GetFileNameWithoutExtension(file))
                End If
            Next

    If RecipeFolder is a variable that is in scope then use this

    Process.Start(Path.Combine(RecipeFolder, RecipeListBox.Text))
    BTW I like simple too yet simple can in many cases get you into trouble which is why I provided the first code sample which is safer. But in the end the choice is yours :-)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 1, 2020 12:41 PM
    Moderator

All replies

  • Give me a few minutes and I will have code for you.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 1, 2020 11:18 AM
    Moderator
  • Okay, we need to "know" the path and file extensions so the following will do this.

    First add this class to your project which will be used to store document information in the ListBox.

    Public Class RecipeListBoxItem
        Public Property FileName() As String
        Public Overrides Function ToString() As String
            Return Path.GetFileName(FileName)
        End Function
    End Class
    

    To load the ListBox

    RecipeListBox.DataSource = Nothing
    Dim fileList As New List(Of RecipeListBoxItem)
    Dim Files() As String = Directory.GetFiles(RecipeFolder)
    For Each file As String In Files
        If Not Path.GetFileName(file).StartsWith("TEMPLATE") Then
            fileList.Add(New RecipeListBoxItem() With {.FileName = file})
        End If
    Next
    RecipeListBox.DataSource = fileList

    We only see the file name, not the path

    To open the selected document

    If RecipeListBox.DataSource IsNot Nothing Then
        If RecipeListBox.SelectedItem IsNot Nothing Then
            Process.Start(CType(RecipeListBox.SelectedItem, RecipeListBoxItem).FileName)
        End If
    End If
    Does this makes sense?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 1, 2020 11:31 AM
    Moderator
  • Im atemting to make sence of this, will get back to you as soon as posable.
    Sunday, March 1, 2020 11:39 AM
  • Im atemting to make sence of this, will get back to you as soon as posable.

    Download the following .zip file which has a form and class file to work with if you need assistance

    https://1drv.ms/u/s!AtGAgKKpqdWjkHol6ZnYFXezfJo4?e=dpYf8F


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 1, 2020 11:55 AM
    Moderator
  • I have atemted this, since I like simple not complecated. 

    Is ther no way of doing it like this?

     RecipeFolder = RecipeFolder & ".docx"
            Dim Files() As String = Directory.GetFiles(RecipeFolder)
    
            For Each file As String In Files
                If Not Path.GetFileName(file).Contains("TEMPLATE") Then
                    RecipeListBox.Items.Add(Path.GetFileNameWithoutExtension(file))
                End If
            Next

    Sunday, March 1, 2020 12:11 PM
  • I have atemted this, since I like simple not complecated. 

    Is ther no way of doing it like this?

     RecipeFolder = RecipeFolder & ".docx"
            Dim Files() As String = Directory.GetFiles(RecipeFolder)
    
            For Each file As String In Files
                If Not Path.GetFileName(file).Contains("TEMPLATE") Then
                    RecipeListBox.Items.Add(Path.GetFileNameWithoutExtension(file))
                End If
            Next

    If RecipeFolder is a variable that is in scope then use this

    Process.Start(Path.Combine(RecipeFolder, RecipeListBox.Text))
    BTW I like simple too yet simple can in many cases get you into trouble which is why I provided the first code sample which is safer. But in the end the choice is yours :-)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 1, 2020 12:41 PM
    Moderator
  • Process.Start(Path.Combine(RecipeFolder, Me.RecipeListBox.Text & ".docx"))

    Thanks Karen.

    Sunday, March 1, 2020 12:58 PM