473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 "" "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
13 4098
jforbes
1,107 Expert 1GB
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
I've tried that and it doesn't seem to work
Aug 20 '14 #3
IronRazer
83 64KB
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
83 64KB
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
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
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
83 64KB
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
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
83 64KB
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
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
83 64KB
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
"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
What is the fn in the For Each fn As String?
Never mind. Found out. Function.
Sep 5 '14 #14

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

Similar topics

1
by: Burl Stafford | last post by:
Is it possible to install 2000 on a flash drive and then use the drive on a laptop that only has 97 installed? I am not allowed to install 2000 on the laptop, but I want to be able to do work with...
5
by: Dustin Davis | last post by:
This might be a pipe dream, but here goes... I'm writing an application many users will be working on and saving files to USB flash drives. When they click the save button, I would like to have...
2
by: Joe Cool | last post by:
I know this is probably not the proper newsgroup to ask this question, but I do not know which one is. And most of you guys here know a lot about windows programming so I am taking a chance some...
3
by: ryancol | last post by:
I have a couple usb 2.0 flash drives. I use them on either windows 2000 or windows XP. When I copy (drag and drop) a file folder to them such as a file folder with like 10 or 20 pictures in it the...
3
kestrel
by: kestrel | last post by:
Hey, I have an 8 gb flash drive. ive been using it to transfer all of my music from one computer (linux) to another (windows xp, if it really matters). After all my transfers were completed, i stuck...
1
by: Olaoluwa | last post by:
I bought one flash drive after some hour it staretd to mist behave what ever i copy is not opening on another computer what can cause it? The other flash is always complain that the flash is fully...
7
by: swapcool | last post by:
Hi, I am a page having a menu and a few frames life below. The links in menu open in F3 frame. I also have back/forward buttons in F1 frame. ________________________ ____________F1_________|...
0
by: Walt Fish | last post by:
I have an application on a flash drive that pulls log files from a hard drive and copies them back to the flash drive. I'm having trouble assigning the proper drive letter to the write/copy process...
1
by: robotarduino | last post by:
I want to read a file from my flash drive called text.csv. However, I cannot even open the port where my flash drive is connected. This is the code that I am using, but I get error since the first...
0
by: janiebont | last post by:
When I first inserted my flash drive it installed one driver but another driver for the "Chipsbnk UDish USB Device"; a message said Windows 7 was not able to find. The flash drive does not show up in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.