Launcher class

Applies to Windows and Windows Phone

Starts the default app associated with the specified file or URI.

Syntax


public static class Launcher

Attributes

[MarshalingBehavior(Standard)]
[Version(0x06020000)]

Members

The Launcher class has these types of members:

Methods

The Launcher class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
LaunchFileAsync(IStorageFile) Starts the default app associated with the specified file.
LaunchFileAsync(IStorageFile, LauncherOptions) Starts the default app associated with the specified file, using the specified options.
LaunchUriAsync(Uri) Starts the default app associated with the URI scheme name for the specified URI.
LaunchUriAsync(Uri, LauncherOptions) Starts the default app associated with the URI scheme name for the specified URI, using the specified options.

 

Remarks

Note  : This class is not agile, which means that you need to consider its threading model and marshaling behavior. For more info, see Threading and Marshaling (C++/CX) and Using Windows Runtime objects in a multithreaded environment (.NET).

Examples

Launch a file contained in the app package

This example uses LaunchFileAsync(IStorageFile) | launchFileAsync(IStorageFile) to launch a file contained in the app package.


async void DefaultLaunch()
{
   // Path to the file in the app package to launch
   string imageFile = @"images\test.png";

   var file = wait Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);

   if (file != null)
   {
      // Launch the retrieved file
      var success = await Windows.System.Launcher.LaunchFileAsync(file);

      if (success)
      {
         // File launched
      }
      else
      {
         // File launch failed
      }
   }
   else
   {
      // Could not find file
   }
}

Launch a URI

This example uses LaunchUriAsync(Uri) | launchUriAsync(Uri) to launch a URI.


// The URI to launch
string uriToLaunch = @"http://www.bing.com";

// Create a Uri object from a URI string 
var uri = new Uri(uriToLaunch);

// Launch the URI
async void DefaultLaunch()
{
   // Launch the URI
   var success = await Windows.System.Launcher.LaunchUriAsync(uri);

   if (success)
   {
      // URI launched
   }
   else
   {
      // URI launch failed
   }
}


Launch with a warning dialog

Call launchUriAsync(Uri, LauncherOptions) to launch a URI with a warning. Use the treatAsUntrusted property to inidcate that the system should display a warning.

Note  

For Windows Store apps using JavaScript, call preventDefault in your event handler if the treatAsUntrusted property is set and you are using an a element to launch the URI.


function linkClickHandler(eventInfo) {
    var link = eventInfo.target;
    if (eventInfo.srcElement && (
        (eventInfo.type === "click") ||
        (eventInfo.type === "keydown" && (
        eventInfo.keyCode === WinJS.Utilities.Key.enter ||
        eventInfo.keyCode === WinJS.Utilities.Key.space)))) {
        eventInfo.preventDefault();
        if (link.href.indexOf("ms-appx") > -1) {
            WinJS.Navigation.navigate(link.href);
        }
        else if (link.href.indexOf("http") > -1) {
            // Create a Uri object from a URI string 
            var uri = new Windows.Foundation.Uri(link.href);
            var options = new Windows.System.LauncherOptions();
            // Launch the URI with a warning prompt
            options.treatAsUntrusted = true;
            // Launch the URI
            Windows.System.Launcher.launchUriAsync(uri, options).then(
                function (success) {
                    if (success) {
                        // URI launched
                    } else {
                        // URI launch failed
                    }
                });
        }
    }
}

Requirements

Minimum supported client

Windows 8 [Windows Store apps only]

Minimum supported server

Windows Server 2012 [Windows Store apps only]

Minimum supported phone

Windows Phone 8

Namespace

Windows.System
Windows::System [C++]

Metadata

Windows.winmd

See also

Tasks
How to launch the default app for a file (JavaScript)
How to launch the default app for a file (C#/VB/C++)
How to launch the default app for a URI (JavaScript)
How to launch the default app for a URI (C#/VB/C++)
Samples
Association launching sample
Guidelines
Guidelines and checklist for file types and URIs
Reference
LauncherOptions
LauncherUIOptions

 

 

Show:
© 2014 Microsoft