views:

86

answers:

4

In VB.net, how can you programmatically launch a .exe file? Is there a way to check if the file is there?

I know there is some way to check using System.IO, but I have no idea. However, I have not even the slightest clue as to how to launch that .exe if it is there, so thanks for the help!

+2  A: 

Check out the System.Diagnostics.Process class. There's a perfect code snippet on the MSDN page, so I won't duplicate it here.

The neat thing about Process is that you can actually launch a file (say, an HTML file) and it will open using the user's default browser. Many common programs accept command-line parameters as well - many browsers accept a URL as a command-line parameter so you can open a specific URL.

Charlie Salts
A: 

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx

openFileDialog od = new OpenFileDialog(); string path = od.ToString() System.Diagnostics.Process.Start(path)

Alynuzzu
That not vb.net, and if it were, that code would not work.
Charlie Salts
Anything with a semicolon is definitely not vb.net lol, at least with my limited knowledge of it.
Cyclone
should I change string Dim, that instead of string path, had to make as Integer Dim path. I`m programming in C# (as default)
Alynuzzu
I stink at porting, sorry.
Cyclone
Even if you did convert to VB.net, you have a typo in the declaration of 'od' and you don't actually call ShowDialog() on od, nor do you check its DialogResult. This could be misleading for people new to VB.net.
Charlie Salts
+2  A: 

Use System.IO.File.Exists and System.Diagnostics.Process.Start.


        Dim someExe As String = "MyAppsPath\some.exe"
        Dim fullPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), someExe)
        If File.Exists(fullPath) Then    'Checks for the existence of the file
            Process.Start(fullPath)      'Executes it
        End If
Alfred Myers
How can I check for the .exe file in /program files/?
Cyclone
I've updated the sample code to get it from %ProgramFiles%
Alfred Myers
@Cyclone: In many cases, it's a good idea to confirm with the user where certain apps are located - My version of TotalCommander lets me specify where my compression utilities are in the settings window. This way, I'm guaranteed to have the correct path, or else the user has made an error, and you can notify them as such. Depending on your needs, this may or may not be helpful. Ultimately, if the program is one you are NOT providing as part of your installation, allow the user to specify where the program is, even if you think you know!
Charlie Salts
The program it would launch is one of mine lol. I guess I could extend it to work with other programs as well?
Cyclone
A: 

You can use System.Diagnostics.Process to execute an .EXE, and can even capture the output if it is a console / command-line application. You would have something like this (but bear in mind this is simplified quite a bit!):

dim process as System.Diagnostics.Process
process.StartInfo.FileName = "program.exe"   ' You need to be smarter here!
process.StartInfo.Arguments = "whatever command line options you need"
process.Start()

To check if the program is still running you call

process.HasExited()

If you want to capture the output you need to set StartInfo.RedirectStandardOutput to True.

AAT