By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

Opening files from a flash drive using combobox and button

P: 9
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 "" "theprogramfolder\theprogram.exe"

I've tried to use

shell "theprogramfolder\theprogram.exe"

but obviously just end up with an error
plus I need it to be used with the combobox so ive done
Expand|Select|Wrap|Line Numbers
  1. ComboBox1.Items.Add(ProgramName1)
  2. ComboBox1.Items.Add(ProgramName2)
  3. ComboBox2.Items.Add(ProgramName3)
  4. ComboBox2.Items.Add(ProgramName4)
under the main form load
and under my button1_click i have
Expand|Select|Wrap|Line Numbers
  1. Select Case ComboBox1.SelectedIndex
  2.     Case 0
  3.          Shell("Program1folder\program1.exe")
  4.     Case 1
  5.          Shell("Program2folder\program2.exe")
  6. End Select
  7. Select Case ComboxBox2.SelectedIndex
  8.     Case 0
  9.          Shell("Program3folder\program3.exe")
  10. '... and so on
  11.  
Thanks for any help
Aug 19 '14 #1
Share this Question
Share on Google+
13 Replies


jforbes
Expert 100+
P: 1,107
I think you are looking for something like:

Expand|Select|Wrap|Line Numbers
  1. Dim procID As Integer
  2. Dim sDirectory as String = My.Application.Info.DirectoryPath
  3.  
  4. 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.
Aug 19 '14 #2

P: 9
I've tried that and it doesn't seem to work
Aug 20 '14 #3

IronRazer
P: 82
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
Expand|Select|Wrap|Line Numbers
  1. Dim MyComboBoxText As String = "SomeFolder\Another Folder\My App.exe"

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         Dim MyComboBoxText As String = "My App.exe"
  5.         FindAndRun(MyComboBoxText)
  6.     End Sub
  7.  
  8.     Private Sub FindAndRun(ByVal filename As String)
  9.         For Each drv As IO.DriveInfo In IO.DriveInfo.GetDrives 'iterate through all the drives on the system
  10.  
  11.             If drv.IsReady AndAlso drv.DriveType = IO.DriveType.Removable Then 'check if the drive is ready and it is a removable drive
  12.  
  13.                 Dim testpath As String = IO.Path.Combine(drv.Name, filename) 'adds the drive name to the filename
  14.  
  15.                 If IO.File.Exists(testpath) Then 'check if the file exists in this removable drive
  16.  
  17.                     Process.Start(testpath) 'use the Process class to start the exe
  18.  
  19.                 End If
  20.             End If
  21.         Next
  22.     End Sub
  23. End Class
  24.  
Aug 21 '14 #4

IronRazer
P: 82
After seeing your other thread i think jforbes has given the correct answer. Use the Application.StartupPath 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)
Aug 21 '14 #5

P: 9
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.
Aug 23 '14 #6

P: 9
So far I have this now just for testing and it obviously doesn't work....

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  4.         ComboBox1.Items.Add("Internet")
  5.     End Sub
  6.  
  7.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  8.         Select Case ComboBox1.SelectedIndex
  9.             Case 0
  10.                 Dim MyComboBoxText As String = "FirefoxPortable\FirefoxPortable.exe"
  11.                 FindAndRun(MyComboBoxText)
  12.         End Select
  13.     End Sub
  14.     Private Sub FindAndRun(ByVal filename As String)
  15.         For Each drv As IO.DriveInfo In IO.DriveInfo.GetDrives
  16.             If drv.IsReady AndAlso drv.DriveType = IO.DriveType.Removable Then
  17.                 Dim testpath As String = IO.Path.Combine
  18.                 If IO.File.Exists(testpath) Then
  19.                     Process.Start(testpath)
  20.                 End If
  21.             End If
  22.         Next
  23.     End Sub
  24. End Class
Aug 23 '14 #7

IronRazer
P: 82
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?
Aug 23 '14 #8

P: 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.
Aug 24 '14 #9

IronRazer
P: 82
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\QuickVal.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.
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     'Adds "Apps" to the directory that this apps exe file was started from
  3.     Private AppsPath As String = IO.Path.Combine(Application.StartupPath, "Apps")
  4.  
  5.     Private ExeFullPaths As New List(Of String) 'This will hold all the full pathnames to the exe files
  6.  
  7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8.         ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList 'make it so user cant type in the combobox
  9.  
  10.         For Each fn As String In IO.Directory.GetFiles(AppsPath, "*.exe") 'iterate through all the exe files in the AppsPath directory
  11.             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
  12.             ComboBox1.Items.Add(IO.Path.GetFileNameWithoutExtension(fn)) 'add just the filename to the combobox
  13.         Next
  14.  
  15.         If ComboBox1.Items.Count > 0 Then ComboBox1.SelectedIndex = 0 'selects the first file if there was any files found and added to combobox
  16.     End Sub
  17.  
  18.     Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  19.         If ComboBox1.SelectedIndex > -1 Then 'makes sure there is an item selected before trying to start one
  20.             Process.Start(ExeFullPaths(ComboBox1.SelectedIndex)) 'start the selected exe file
  21.         End If
  22.     End Sub
  23. End Class
  24.  
Aug 31 '14 #10

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

************** Exception Text **************
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\UserGuy\AppData\Local\Apps\2.0\5Y709M5T. CKQ\QV43NDT4.2QV\test..tion_5e6ec928ad2744d3_0001. 0000_7a4b9744597780e5\Apps'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileSystemEnumerableIterator`1.CommonIni t()
at System.IO.FileSystemEnumerableIterator`1..ctor(Str ing path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
at System.IO.Directory.GetFiles(String path, String searchPattern)
at Tester_BS_3.Form1.Form1_Load(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr 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_5e6ec928ad2744d3_0001.0000_7a4b97445977 80e5/Tester%20BS%203.exe
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 10.0.0.0
Win32 Version: 12.0.20806.33440 built by: FX45W81RTMREL
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.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__b77a5c561934e089/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__b77a5c561934e089/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__b77a5c561934e089/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__b03f5f7f11d50a3a/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__b77a5c561934e089/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:

<configuration>
<system.windows.forms jitDebugging="true" />
</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.
Sep 4 '14 #11

IronRazer
P: 82
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.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\UserGuy\AppData\Local\Apps\2.0\5Y709M5T. CKQ\QV43NDT4.2QV\test..tion_5e6ec928ad2744d3_0001. 0000_7a4b9744597780e5\Apps'.

The path looks kind of strange to me. I am not sure why it is showing the AppData\Local\Apps..... 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.
Sep 5 '14 #12

P: 9
"The path looks kind of strange to me. I am not sure why it is showing the AppData\Local\Apps..... 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.
Sep 5 '14 #13

P: 9
What is the fn in the For Each fn As String?
Never mind. Found out. Function.
Sep 5 '14 #14

Post your reply

Sign in to post your reply or Sign up for a free account.