Launcher.LaunchFileAsync 方法

定义

重载

LaunchFileAsync(IStorageFile)

启动与指定文件关联的默认应用。

LaunchFileAsync(IStorageFile, LauncherOptions)

使用指定的选项启动与指定文件关联的默认应用。

LaunchFileAsync(IStorageFile)

启动与指定文件关联的默认应用。

public:
 static IAsyncOperation<bool> ^ LaunchFileAsync(IStorageFile ^ file);
/// [Windows.Foundation.Metadata.Overload("LaunchFileAsync")]
 static IAsyncOperation<bool> LaunchFileAsync(IStorageFile const& file);
[Windows.Foundation.Metadata.Overload("LaunchFileAsync")]
public static IAsyncOperation<bool> LaunchFileAsync(IStorageFile file);
function launchFileAsync(file)
Public Shared Function LaunchFileAsync (file As IStorageFile) As IAsyncOperation(Of Boolean)

参数

file
IStorageFile

文件。

返回

启动操作。

属性

示例

此示例使用 LaunchFileAsync (IStorageFile) 启动应用包中包含的文件。

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

   var file = await 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
   }
}
Windows::Foundation::IAsyncAction MainPage::DefaultLaunch()
{
    // Get the app's installation folder.
    Windows::Storage::StorageFolder installFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };

    Windows::Storage::StorageFile file{ co_await installFolder.GetFileAsync(L"Assets\\LockScreenLogo.scale-200.png") };

    if (file)
    {
        // Launch the retrieved file.
        bool success{ co_await Windows::System::Launcher::LaunchFileAsync(file) };
        if (success)
        {
            // File launched.
        }
        else
        {
            // File launch failed.
        }
    }
    else
    {
        // Couldn't find file.
    }
}
void MainPage::DefaultLaunch()
{
   auto installFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;

   concurrency::task<Windows::Storage::StorageFile^> getFileOperation(installFolder->GetFileAsync("images\\test.png"));
   getFileOperation.then([](Windows::Storage::StorageFile^ file)
   {
      if (file != nullptr)
      {
         // Launch the retrieved file
         concurrency::task<bool> launchFileOperation(Windows::System::Launcher::LaunchFileAsync(file));
         launchFileOperation.then([](bool success)
         {
            if (success)
            {
               // File launched
            }
            else
            {
               // File launch failed
            }
         });
      }
      else
      {
         // Could not find file
      }
   });
}
async Sub DefaultLaunch()

   ' Path to the file in the app package to launch
   Dim imageFile = "images\test.png"

   Dim file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile)

   If file IsNot Nothing Then
      ' Launch the retrieved file
      Dim success = await Windows.System.Launcher.LaunchFileAsync(file)

      If success Then
         ' File launched
      Else
         ' File launch failed
      End If
   Else
      ' Could not find file
   End If
End Sub

注解

调用 API 时,调用应用必须对用户可见。

必须从 ASTA 线程 (也称为 UI 线程) 调用此 API。

此 API 还对其可以启动的文件类型施加了一些限制。 阻止许多包含可执行代码的文件类型(例如,.exe、.msi 和 .js 文件)启动。 此限制可保护用户免受可能修改系统的潜在恶意文件的侵害。

当启动因上述任何原因失败时,API 会成功,并从其异步操作中返回 FALSE。 由于它无法查询上述限制是否适用于当前启动,因此调用应用不应假定启动成功,并且应在失败时提供回退机制。 一种可能的解决方案是要求用户保存文件,并指示用户在桌面上打开它。

若要使用户能够选择应用而不是启动默认应用,请设置 LauncherOptions.DisplayApplicationPicker 属性。

若要显示文件可能不安全的警告,请设置 LauncherOptions.TreatAsUntrusted 属性。

文件将传递到关联的应用。 如果关联的应用是桌面应用,则使用 shell 执行机制传递文件。

另请参阅

适用于

LaunchFileAsync(IStorageFile, LauncherOptions)

使用指定的选项启动与指定文件关联的默认应用。

public:
 static IAsyncOperation<bool> ^ LaunchFileAsync(IStorageFile ^ file, LauncherOptions ^ options);
/// [Windows.Foundation.Metadata.Overload("LaunchFileWithOptionsAsync")]
 static IAsyncOperation<bool> LaunchFileAsync(IStorageFile const& file, LauncherOptions const& options);
[Windows.Foundation.Metadata.Overload("LaunchFileWithOptionsAsync")]
public static IAsyncOperation<bool> LaunchFileAsync(IStorageFile file, LauncherOptions options);
function launchFileAsync(file, options)
Public Shared Function LaunchFileAsync (file As IStorageFile, options As LauncherOptions) As IAsyncOperation(Of Boolean)

参数

file
IStorageFile

文件。

options
LauncherOptions

应用的启动选项。

返回

启动操作。

属性

示例

调用 [Launcher.LaunchFileAsync (IStorageFile, LauncherOptions) 方法,并将 LauncherOptions.DisplayApplicationPicker 设置为 true ,以启动用户从“ 打开方式 ”对话框中为文件选择的应用。

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

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

   if (file != null)
   {
      // Set the option to show the picker
      var options = new Windows.System.LauncherOptions();
      options.DisplayApplicationPicker = true;

      // Launch the retrieved file
      bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
      if (success)
      {
         // File launched
      }
      else
      {
         // File launch failed
      }
   }
   else
   {
      // Could not find file
   }
}
Windows::Foundation::IAsyncAction MainPage::DefaultLaunch()
{
    // Get the app's installation folder.
    Windows::Storage::StorageFolder installFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };

    Windows::Storage::StorageFile file{ co_await installFolder.GetFileAsync(L"Assets\\LockScreenLogo.scale-200.png") };

    if (file)
    {
        // Set the option to show the picker.
        Windows::System::LauncherOptions launcherOptions;
        launcherOptions.DisplayApplicationPicker(true);

        // Launch the retrieved file.
        bool success{ co_await Windows::System::Launcher::LaunchFileAsync(file, launcherOptions) };
        if (success)
        {
            // File launched.
        }
        else
        {
            // File launch failed.
        }
    }
    else
    {
        // Couldn't find file.
    }
}
void MainPage::DefaultLaunch()
{
   auto installFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;

   concurrency::task<Windows::Storage::StorageFile^> getFileOperation(installFolder->GetFileAsync("images\\test.png"));
   getFileOperation.then([](Windows::Storage::StorageFile^ file)
   {
      if (file != nullptr)
      {
         // Set the option to show the picker
         auto launchOptions = ref new Windows::System::LauncherOptions();
         launchOptions->DisplayApplicationPicker = true;

         // Launch the retrieved file
         concurrency::task<bool> launchFileOperation(Windows::System::Launcher::LaunchFileAsync(file, launchOptions));
         launchFileOperation.then([](bool success)
         {
            if (success)
            {
               // File launched
            }
            else
            {
               // File launch failed
            }
         });
      }
      else
      {
         // Could not find file
      }
   });
}
async Sub DefaultLaunch()

   ' Path to the file in the app package to launch
   Dim imageFile = "images\test.png"

   Dim file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile)

   If file IsNot Nothing Then
      ' Set the option to show the picker
      Dim options = Windows.System.LauncherOptions()
      options.DisplayApplicationPicker = True

      ' Launch the retrieved file
      Dim success = await Windows.System.Launcher.LaunchFileAsync(file, options)

      If success Then
         ' File launched
      Else
         ' File launch failed
      End If
   Else
      ' Could not find file
   End If
End Sub

注解

调用 API 时,调用应用必须对用户可见。

必须从 ASTA 线程内部调用此 API, (也称为 UI 线程) 。

此 API 还对其可以启动的文件类型施加了一些限制。 阻止许多包含可执行代码的文件类型(例如,.exe、.msi 和 .js 文件)启动。 此限制可保护用户免受可能修改系统的潜在恶意文件的侵害。

当启动因上述任何原因失败时,API 会成功,并从其异步操作中返回 FALSE。 由于它无法查询上述限制是否适用于当前启动,因此调用应用不应假定启动成功,并且应在失败时提供回退机制。 一种可能的解决方案是要求用户保存文件,并指示用户在桌面上打开它。

若要使用户能够选择应用而不是启动默认应用,请设置 LauncherOptions.DisplayApplicationPicker 属性。

若要显示文件可能不安全的警告,请设置 LauncherOptions.TreatAsUntrusted 属性。

文件将传递到关联的应用。 如果关联的应用是桌面应用,则使用 shell 执行机制传递文件。

另请参阅

适用于