Recipe 14.4. Starting Other Applications by EXE, Document, or URL


Problem

You need to start up a separate application, based on either the path to the executable program file, a document with a registered file extension, or a valid URL for a web page or other resource.

Solution

Use the System.Diagnostics.Process.Start() method to initiate applications external to your own application.

Discussion

The Start() method returns an object of type System.Diagnostics.Process that encapsulates the newly started application. Process.Start() works with three types of targets:

  • If you know the path to the executable (EXE) file, you can specify it using the first argument to Process.Start( ). If you don't supply a full path, Windows will search through the path defined for the current user for the program. Any additional command-line arguments appear in the second argument:

     ' ----- Start up a new Notepad window. Process.Start("C:\Windows\Notepad.exe") ' ----- Excluding the path and extension works. Process.Start("Notepad") ' ----- Open a specific file through Notepad. Process.Start("Notepad.exe", "C:\DataFile.txt") 

  • You can start an application associated with a registered file extension by specifying a file with that extension as the argument:

     ' ----- Open Notepad with a specific file. Process.Start("C:\DataFile.txt") 

    The file must already exist and must have a valid registered file extension, or an exception will occur.

  • You can specify any URL, including a web page or email address (in a mailto:// URL). Any of the accepted URL prefixes, such as http://, mailto://, or file://, can be included in the URL:

     ' ----- Open a specific web page in the default browser. Process.Start("http://www.microsoft.com") 

The arguments passed to Process.Start() are similar to those you would enter in the Windows Start Run menu command prompt, or in the Windows Command Prompt using the Start command.

The Process object returned by Process.Start() includes several properties and methods that let you monitor and control (somewhat) the new process. To force the new process to exit, use the Process object's Kill() method.

Visual Basic also includes another command from its pre-.NET days that starts up external applications. The Shell() function accepts two arguments: the command and the window style. The command is the executable filename of the program to run, with any command-line arguments included. The second argument uses the members of the Microsoft.VisualBasic.AppWinStyle enumeration to indicate whether the new program's main window should start as maximized, minimized, or normal, and whether it should immediately receive the input focus. Here are the choices:

  • AppWinStyle.Hide

  • AppWinStyle.MaximizedFocus

  • AppWinStyle.MinimizedFocus

  • AppWinStyle.MinimizedNoFocus

  • AppWinStyle.NormalFocus

  • AppWinStyle.NormalNoFocus

For example, to start up Notepad with a specific file open, use this command:

 Shell("Notepad.exe C:\DataFile.txt", _    AppWinStyle.NormalFocus) 

You can use only executable programs with Shell(). It does not accept URLs or files with registered extensions.

See Also

Recipe 14.5 shows how to wait for the newly started process to complete before continuing with the main program.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net