Opening files from a flash drive using combobox and button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbcompgeek
    New Member
    • Aug 2014
    • 9

    Opening files from a flash drive using combobox and button

    I've been trying to open some applications (.exe) that are on my flash drive using multiple ComboBox's to select the different types of programs and a button to lauch the selected program. My issue is the flash drive wont always be e:\. I need my program to open the files from the flash drives current directory. I've done it in batch real easy...Visual Basic I'm having trouble. In batch or cmd its as easy as this

    start "" "theprogramfold er\theprogram.e xe"

    I've tried to use

    shell "theprogramfold er\theprogram.e xe"

    but obviously just end up with an error
    plus I need it to be used with the combobox so ive done
    Code:
    ComboBox1.Items.Add(ProgramName1)
    ComboBox1.Items.Add(ProgramName2)
    ComboBox2.Items.Add(ProgramName3)
    ComboBox2.Items.Add(ProgramName4)
    under the main form load
    and under my button1_click i have
    Code:
    Select Case ComboBox1.SelectedIndex
        Case 0
             Shell("Program1folder\program1.exe")
        Case 1
             Shell("Program2folder\program2.exe")
    End Select
    Select Case ComboxBox2.SelectedIndex
        Case 0
             Shell("Program3folder\program3.exe")
    '... and so on
    Thanks for any help
    Last edited by Frinavale; Sep 16 '14, 04:57 PM. Reason: Added code tags
  • jforbes
    Recognized Expert Top Contributor
    • Aug 2014
    • 1107

    #2
    I think you are looking for something like:

    Code:
    Dim procID As Integer
    Dim sDirectory as String = My.Application.Info.DirectoryPath
    
    procID = Shell(sDirectory  & "\program1.exe", AppWinStyle.NormalFocus)
    I don't have Visual Studio in front of me right now, so I haven't verified the code.

    Comment

    • pbcompgeek
      New Member
      • Aug 2014
      • 9

      #3
      I've tried that and it doesn't seem to work

      Comment

      • IronRazer
        New Member
        • Jan 2013
        • 83

        #4
        Hi,
        If your worried about finding the usb drive that the exe is on then you can loop through all the Removable drives on the system and check if your file exists on the drive. If it exists then use the Process class to run your exe file.

        If your exe file is located in a subfolder on the usb drive then you will need to supply the full path without the drive letter like this
        Code:
        Dim MyComboBoxText As String = "SomeFolder\Another Folder\My App.exe"

        Code:
        Public Class Form1
        
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                Dim MyComboBoxText As String = "My App.exe"
                FindAndRun(MyComboBoxText)
            End Sub
        
            Private Sub FindAndRun(ByVal filename As String)
                For Each drv As IO.DriveInfo In IO.DriveInfo.GetDrives 'iterate through all the drives on the system
        
                    If drv.IsReady AndAlso drv.DriveType = IO.DriveType.Removable Then 'check if the drive is ready and it is a removable drive
        
                        Dim testpath As String = IO.Path.Combine(drv.Name, filename) 'adds the drive name to the filename
        
                        If IO.File.Exists(testpath) Then 'check if the file exists in this removable drive
        
                            Process.Start(testpath) 'use the Process class to start the exe
        
                        End If
                    End If
                Next
            End Sub
        End Class

        Comment

        • IronRazer
          New Member
          • Jan 2013
          • 83

          #5
          After seeing your other thread i think jforbes has given the correct answer. Use the Application.Sta rtupPath to get the path to the directory your app is running from. Add your filename to it and run it.

          I would use IO.Path.Combine to add your filename to your path. I would also recommend using the Process class instead of the Shell command. 8)

          Comment

          • pbcompgeek
            New Member
            • Aug 2014
            • 9

            #6
            Thank you for replying IronRazer. I've tried what jforbes said and that didnt work. I would like to try what you sent. Do you think it will work or what do you think I should try exactly. Sorry little confused. Also I'm trying to launch multiple different app.exe's from different ComboBox's and just so you know I'm working with Microsoft Visual Express 2013.

            Comment

            • pbcompgeek
              New Member
              • Aug 2014
              • 9

              #7
              So far I have this now just for testing and it obviously doesn't work....

              Code:
              Public Class Form1
              
                  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                      ComboBox1.Items.Add("Internet")
                  End Sub
              
                  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
                      Select Case ComboBox1.SelectedIndex
                          Case 0
                              Dim MyComboBoxText As String = "FirefoxPortable\FirefoxPortable.exe"
                              FindAndRun(MyComboBoxText)
                      End Select
                  End Sub
                  Private Sub FindAndRun(ByVal filename As String)
                      For Each drv As IO.DriveInfo In IO.DriveInfo.GetDrives
                          If drv.IsReady AndAlso drv.DriveType = IO.DriveType.Removable Then
                              Dim testpath As String = IO.Path.Combine
                              If IO.File.Exists(testpath) Then
                                  Process.Start(testpath)
                              End If
                          End If
                      Next
                  End Sub
              End Class

              Comment

              • IronRazer
                New Member
                • Jan 2013
                • 83

                #8
                Is your Main application`s exe file that has the Comboboxes on it located on the same flash drive? Is the exe files that your trying to execute in the same directory as the main applications exe?

                Comment

                • pbcompgeek
                  New Member
                  • Aug 2014
                  • 9

                  #9
                  It's not yet I've just been debugging and testing that way. Should I be publishing it and be putting on the flash drive? And to your second question ill be putting the exe for the program on the main part of the flash drive and programs in subfolders on the flash drive. Thank you so much for helping btw.

                  Comment

                  • IronRazer
                    New Member
                    • Jan 2013
                    • 83

                    #10
                    Your going to want to make a folder in your projects Debug folder that contains the exe files you want to have listed in your combobox to run for now if your projects exe is not on the thumb drive where the subfolder with exe files are located. Then later you can copy your projects exe file and the folder containing the exe`s to the thumb drive.

                    You need to have some sort of a fixed way to find the exe files from your app.

                    For example my test project is called "Run Exe from subfolder on thumb drive.exe" and is located directly on my thumb drive. I also made a folder on the thumb drive called "Apps" that i put a few exe files in that i want to run from my app. So the paths look like this.

                    My test project:
                    E:\Run Exe from subfolder on thumb drive.exe

                    The exe paths for the programs i want to run:
                    E:\Apps\Color Picker.exe
                    E:\Apps\QuickVa l.exe

                    My test project is a simple form with 1 ComboBox and 1 Button on it and uses the code below. When it is run it will list all the exe files in the Apps folder. Then when i select a program in the combobox and press the button it will run that program from the Apps folder.
                    Code:
                    Public Class Form1
                        'Adds "Apps" to the directory that this apps exe file was started from
                        Private AppsPath As String = IO.Path.Combine(Application.StartupPath, "Apps")
                    
                        Private ExeFullPaths As New List(Of String) 'This will hold all the full pathnames to the exe files
                    
                        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList 'make it so user cant type in the combobox
                    
                            For Each fn As String In IO.Directory.GetFiles(AppsPath, "*.exe") 'iterate through all the exe files in the AppsPath directory
                                ExeFullPaths.Add(fn) 'add the full path to a list so you can start the exe file depending on the selected index of the combobox
                                ComboBox1.Items.Add(IO.Path.GetFileNameWithoutExtension(fn)) 'add just the filename to the combobox
                            Next
                    
                            If ComboBox1.Items.Count > 0 Then ComboBox1.SelectedIndex = 0 'selects the first file if there was any files found and added to combobox
                        End Sub
                    
                        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                            If ComboBox1.SelectedIndex > -1 Then 'makes sure there is an item selected before trying to start one
                                Process.Start(ExeFullPaths(ComboBox1.SelectedIndex)) 'start the selected exe file
                            End If
                        End Sub
                    End Class

                    Comment

                    • pbcompgeek
                      New Member
                      • Aug 2014
                      • 9

                      #11
                      See the end of this message for details on invoking
                      just-in-time (JIT) debugging instead of this dialog box.

                      ************** Exception Text **************
                      System.IO.Direc toryNotFoundExc eption: Could not find a part of the path 'C:\Users\UserG uy\AppData\Loca l\Apps\2.0\5Y70 9M5T.CKQ\QV43ND T4.2QV\test..ti on_5e6ec928ad27 44d3_0001.0000_ 7a4b9744597780e 5\Apps'.
                      at System.IO.__Err or.WinIOError(I nt32 errorCode, String maybeFullPath)
                      at System.IO.FileS ystemEnumerable Iterator`1.Comm onInit()
                      at System.IO.FileS ystemEnumerable Iterator`1..cto r(String path, String originalUserPat h, String searchPattern, SearchOption searchOption, SearchResultHan dler`1 resultHandler, Boolean checkHost)
                      at System.IO.Direc tory.GetFiles(S tring path, String searchPattern)
                      at Tester_BS_3.For m1.Form1_Load(O bject sender, EventArgs e)
                      at System.EventHan dler.Invoke(Obj ect sender, EventArgs e)
                      at System.Windows. Forms.Form.OnLo ad(EventArgs e)
                      at System.Windows. Forms.Form.OnCr eateControl()
                      at System.Windows. Forms.Control.C reateControl(Bo olean fIgnoreVisible)
                      at System.Windows. Forms.Control.C reateControl()
                      at System.Windows. Forms.Control.W mShowWindow(Mes sage& m)
                      at System.Windows. Forms.Control.W ndProc(Message& m)
                      at System.Windows. Forms.Scrollabl eControl.WndPro c(Message& m)
                      at System.Windows. Forms.Container Control.WndProc (Message& m)
                      at System.Windows. Forms.Form.WmSh owWindow(Messag e& m)
                      at System.Windows. Forms.Form.WndP roc(Message& m)
                      at System.Windows. Forms.Control.C ontrolNativeWin dow.OnMessage(M essage& m)
                      at System.Windows. Forms.Control.C ontrolNativeWin dow.WndProc(Mes sage& m)
                      at System.Windows. Forms.NativeWin dow.Callback(In tPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


                      ************** Loaded Assemblies **************
                      mscorlib
                      Assembly Version: 4.0.0.0
                      Win32 Version: 4.0.30319.34014 built by: FX45W81RTMGDR
                      CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
                      ----------------------------------------
                      Tester BS 3
                      Assembly Version: 1.0.0.0
                      Win32 Version: 1.0.0.0
                      CodeBase: file:///C:/Users/UserGuy/AppData/Local/Apps/2.0/5Y709M5T.CKQ/QV43NDT4.2QV/test..tion_5e6e c928ad2744d3_00 01.0000_7a4b974 4597780e5/Tester%20BS%203 .exe
                      ----------------------------------------
                      Microsoft.Visua lBasic
                      Assembly Version: 10.0.0.0
                      Win32 Version: 12.0.20806.3344 0 built by: FX45W81RTMREL
                      CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Visua lBasic/v4.0_10.0.0.0__ b03f5f7f11d50a3 a/Microsoft.Visua lBasic.dll
                      ----------------------------------------
                      System
                      Assembly Version: 4.0.0.0
                      Win32 Version: 4.0.30319.34003 built by: FX45W81RTMGDR
                      CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b 77a5c561934e089/System.dll
                      ----------------------------------------
                      System.Core
                      Assembly Version: 4.0.0.0
                      Win32 Version: 4.0.30319.33440 built by: FX45W81RTMREL
                      CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b 77a5c561934e089/System.Core.dll
                      ----------------------------------------
                      System.Windows. Forms
                      Assembly Version: 4.0.0.0
                      Win32 Version: 4.0.30319.33440 built by: FX45W81RTMREL
                      CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows. Forms/v4.0_4.0.0.0__b 77a5c561934e089/System.Windows. Forms.dll
                      ----------------------------------------
                      System.Drawing
                      Assembly Version: 4.0.0.0
                      Win32 Version: 4.0.30319.33440 built by: FX45W81RTMREL
                      CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b 03f5f7f11d50a3a/System.Drawing. dll
                      ----------------------------------------
                      System.Runtime. Remoting
                      Assembly Version: 4.0.0.0
                      Win32 Version: 4.0.30319.34107 built by: FX45W81RTMGDR
                      CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Runtime. Remoting/v4.0_4.0.0.0__b 77a5c561934e089/System.Runtime. Remoting.dll
                      ----------------------------------------

                      ************** JIT Debugging **************
                      To enable just-in-time (JIT) debugging, the .config file for this
                      application or computer (machine.config ) must have the
                      jitDebugging value set in the system.windows. forms section.
                      The application must also be compiled with debugging
                      enabled.

                      For example:

                      <configuratio n>
                      <system.windows .forms jitDebugging="t rue" />
                      </configuration>

                      When JIT debugging is enabled, any unhandled exception
                      will be sent to the JIT debugger registered on the computer
                      rather than be handled by this dialog box.

                      I did all that and this is what is happening now.

                      Comment

                      • IronRazer
                        New Member
                        • Jan 2013
                        • 83

                        #12
                        If you look at the very first part of your error message it says that it can`t find the the "Apps" directory.

                        System.IO.Direc toryNotFoundExc eption: Could not find a part of the path 'C:\Users\UserG uy\AppData\Loca l\Apps\2.0\5Y70 9M5T. CKQ\QV43NDT4.2Q V\test..tion_5e 6ec928ad2744d3_ 0001. 0000_7a4b974459 7780e5\Apps'.

                        The path looks kind of strange to me. I am not sure why it is showing the AppData\Local\A pps..... and so on for the path.

                        Are you sure you made a folder called "Apps" in the same directory that your projects exe file is located in? If your still running your project from the VB editor then your "Apps" folder needs to be in the Debug folder of your projects folder.

                        If you have copied your projects exe file to the root of your thumb drive then you need the "Apps" folder on the root of the thumb drive also.

                        Comment

                        • pbcompgeek
                          New Member
                          • Aug 2014
                          • 9

                          #13
                          "The path looks kind of strange to me. I am not sure why it is showing the AppData\Local\A pps..... and so on for the path. "

                          I don't know why it was either. I have a folder on my flash drive called Apps and in the debug folder on my computer so when I run debugging it works. I've just wrote what you've wrote above just as a test program one combobox and button. When I publish to the flash drive and try to install from flash drive the install fails. I'm also creating the windows form app on the flash drive when i first create the program.
                          Thanks for the help.

                          Comment

                          • pbcompgeek
                            New Member
                            • Aug 2014
                            • 9

                            #14
                            What is the fn in the For Each fn As String?
                            Never mind. Found out. Function.

                            Comment

                            Working...