Results 1 to 7 of 7

Thread: Run an app from a ListBox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2021
    Posts
    26

    Run an app from a ListBox

    I have an app in vb.net that pulls a list of files from a directory and displays them in a ListBox. These files could be .pdf, .mov, .exe or .docx files. When I select a file from the list, I want it to either display the contents of the .pdf and .docx files or run the .exe and .mov files.

    The code builds the ListBox properly, but when I select a file, I get the following error message:

    {"An error occurred trying to start process 'F:\WoodWorking\Adirondack_chair _woodrequirements.docx' with working directory 'F:\Woodworking\WWDISPLAY\WoodWorkingFiles\bin\Debug\net6.0-windows'. The specified executable is not a valid application for this OS platform."}

    I get the same error message regardless of file type.

    Attached is the code in its entirety any working assistance would be greatly appreciated.


    Imports System.IO

    Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub WoodWorking_SelectedIndexChanged(sender As Object, e As
    EventArgs) Handles WoodWorking.SelectedIndexChanged
    End Sub

    Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles
    BtnStart.Click
    Dim finfo As New IO.DirectoryInfo("F:\WoodWorking" )
    WoodWorking.Items.Clear()
    For Each fi In finfo.GetFiles("*.*" )
    WoodWorking.Items.Add(Path.GetFileName(fi.FullName))
    Next
    End Sub

    Private Sub WoodWorking_DoubleClick(sender As Object, e As EventArgs) Handles
    WoodWorking.DoubleClick
    Dim fullPath = Path.Combine("F:\WoodWorking" ,
    WoodWorking.SelectedItem.ToString())
    System.Diagnostics.Process.Start(fullPath)
    End Sub

    End Class

  2. #2
    Fanatic Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    630

    Re: Run an app from a ListBox

    You'll need to check the file extension of the selected file and then run the appropriate code to either display the file or launch an EXE.

    Do you want to display the contents of the Word or PDF file in a control on your Winform? Similarly, display the video on your form or are you looking to launch Word to display the Document, a PDF viewer for the PDF and a video player for the .mov file?

  3. #3
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,525

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2021
    Posts
    26

    Re: Run an app from a ListBox

    I have used this same basic concept on another project, the only difference is that I included a Textbox to enter a filename to lookup. The app worked as expected, I didn't need to set up for any of the file extensions. When you selected a file, regardless of the extension, it ran it. I went back to the older project, refitted it for the new directory and it runs perfectly. So I'll just copy the code from the older app and rewrite it to fitt the new criteria.

    I'm not sure why it wouldn't run as originally written but someday I'll work it out.

    Thanks for the reply.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Run an app from a ListBox

    The links provided in post #3 probably address this but the issue is almost certainly due to different default behaviour between .NET Framework and .NET Core (.NET 5 and later are based on .NET Core). In order to open a data file in its default application using Process.Start, you need to set UseShellExecute to True. That is the default in .NET Framework but not in .NET Core. You will need to use an overload of Process.Start that takes a ProcessStartInfo as an argument and set UseShellExecute to True on that object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Fanatic Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    630

    Re: Run an app from a ListBox

    Quote Originally Posted by Darby View Post
    I have used this same basic concept on another project, the only difference is that I included a Textbox to enter a filename to lookup. The app worked as expected, I didn't need to set up for any of the file extensions. When you selected a file, regardless of the extension, it ran it. I went back to the older project, refitted it for the new directory and it runs perfectly. So I'll just copy the code from the older app and rewrite it to fitt the new criteria.

    I'm not sure why it wouldn't run as originally written but someday I'll work it out.

    Thanks for the reply.
    Ok, this
    I select a file from the list, I want it to either display the contents of the .pdf and .docx files or run the .exe and .mov files.
    was a bit confusing as to whether you wanted to display it embedded on your form or launch the default app for the file type. That is why I asked the question.

    jmcilhinney's answer will get you where you want to go.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2021
    Posts
    26

    Re: Run an app from a ListBox

    I want to launch the default app. I rewrote the original code to fit the new app and it works perfectly. the only change I made was changing the directory in finfo. I also agree with jmcilhinney's answer. Thanks to all who responded.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width