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 が呼び出されたときにユーザーに表示される必要があります。

この API は、ASTA スレッド (UI スレッドとも呼ばれます) から呼び出す必要があります。

この API では、起動できるファイルの種類にもいくつかの制限があります。 .exe、.msi、.js ファイルなど、実行可能コードを含む多くのファイルの種類は、起動をブロックされます。 この制限は、システムを変更する可能性のある悪意のあるファイルからユーザーを保護します。

上記のいずれかの理由で起動が失敗すると、API は成功し、非同期操作から FALSE を返します。 上記の制限が現在の起動に適用されるかどうかを照会する機能がないため、呼び出し元のアプリは起動が成功したと想定せず、失敗した場合にフォールバック メカニズムを提供する必要があります。 考えられる解決策は、ファイルを保存し、デスクトップで開くようにユーザーに指示することです。

ユーザーが既定のアプリを起動する代わりにアプリを選択できるようにするには、 LauncherOptions.DisplayApplicationPicker プロパティを設定します。

ファイルが安全でない可能性があることを示す警告を表示するには、 LauncherOptions.TreatAsUntrusted プロパティを設定します。

ファイルは、関連付けられているアプリに渡されます。 関連付けられているアプリがデスクトップ アプリの場合、ファイルはシェル実行メカニズムを使用して渡されます。

こちらもご覧ください

適用対象

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

アプリの起動オプション。

戻り値

起動操作。

属性

LauncherOptions.DisplayApplicationPickertrue に設定して [Launcher.LaunchFileAsync(IStorageFile, LauncherOptions) メソッドを呼び出して、[ファイルを開く] ダイアログ ボックスからファイル用に選択したアプリを起動します。

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 が呼び出されたときにユーザーに表示される必要があります。

この API は、ASTA スレッド (UI スレッドとも呼ばれます) 内から呼び出す必要があります。

この API では、起動できるファイルの種類にもいくつかの制限があります。 .exe、.msi、.js ファイルなど、実行可能コードを含む多くのファイルの種類は、起動をブロックされます。 この制限は、システムを変更する可能性のある悪意のあるファイルからユーザーを保護します。

上記のいずれかの理由で起動が失敗すると、API は成功し、非同期操作から FALSE を返します。 上記の制限が現在の起動に適用されるかどうかを照会する機能がないため、呼び出し元のアプリは起動が成功したと想定せず、失敗した場合にフォールバック メカニズムを提供する必要があります。 考えられる解決策は、ファイルを保存し、デスクトップで開くようにユーザーに指示することです。

ユーザーが既定のアプリを起動する代わりにアプリを選択できるようにするには、 LauncherOptions.DisplayApplicationPicker プロパティを設定します。

ファイルが安全でない可能性があることを示す警告を表示するには、 LauncherOptions.TreatAsUntrusted プロパティを設定します。

ファイルは、関連付けられているアプリに渡されます。 関連付けられているアプリがデスクトップ アプリの場合、ファイルはシェル実行メカニズムを使用して渡されます。

こちらもご覧ください

適用対象