Manage .exe files
In VB.net, how can you programmatically run an .exe file? Is there a way to check if the file is there?
I know there is a way to check using System.IO, but I have no idea. However, I don't even know how to run this .exe if it is there, so thanks for the help!
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
Check out the System.Diagnostics.Process class . The MSDN page has a perfect code snippet, so I won't duplicate it here.
The best thing about Process is that you can actually run a file (say an HTML file) and it will open using the user's default browser. Many common programs also accept command line parameters - many browsers accept a URL as a command line parameter, so you can open a specific URL.
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)
You can use System.Diagnostics.Process to execute the .EXE and even capture the output if it is a console / command line application. You will have something like this (but keep in mind that this is simplified 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 running, you call
process.HasExited()
If you want to capture the output, you need to set StartInfo.RedirectStandardOutput to True.