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 執行緒呼叫此 API, (也稱為 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

應用程式的啟動選項。

傳回

啟動作業。

屬性

範例

呼叫 [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 屬性。

檔案會傳遞至相關聯的應用程式。 如果相關聯的應用程式是傳統型應用程式,則會使用殼層執行機制傳遞檔案。

另請參閱

適用於