Share via


如何啟動檔案的預設應用程式 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

了解如何啟動檔案的預設應用程式。許多應用程式需要使用它們本身無法處理的檔案。例如,電子郵件應用程式會收到各種類型的檔案,因此它們需要可以使用這些檔案類型的預設處理常式啟動這些檔案的方法。

這些步驟示範如何使用 Windows.System.Launcher API 來啟動您應用程式本身無法處理之檔案的預設處理常式。

指示

步驟 1: 取得檔案

首先,為檔案取得 Windows.Storage.StorageFile 物件。

如果檔案包含在應用程式的套件中,您可以使用 Package.installedLocation 屬性來取得 Windows.Storage.StorageFolder 物件,並使用 Windows.Storage.StorageFolder.getFileAsync 方法來取得 StorageFile 物件。

如果檔案位於已知資料夾中,您可以使用 Windows.Storage.KnownFolders 類別的屬性來取得 StorageFolder,並使用 getFileAsync 方法來取得 StorageFile 物件。

步驟 2: 啟動檔案

Windows 提供數個不同的選項來啟動檔案的預設處理常式。下列圖表及後續小節會分別說明它們。

選項 方法 說明
預設啟動 LaunchFileAsync(IStorageFile) 使用預設處理常式啟動指定的檔案。
開啟檔案啟動 LaunchFileAsync(IStorageFile, LauncherOptions) 啟動指定的檔案,讓使用者透過 [開啟檔案] 對話方塊挑選處理常式。
使用建議的應用程式備用選項啟動 LaunchFileAsync(IStorageFile, LauncherOptions) 使用預設處理常式啟動指定的檔案。如果系統上沒有安裝處理常式,則建議使用者使用市集中的應用程式。
以所需的剩餘檢視啟動 LaunchFileAsync(IStorageFile, LauncherOptions) (僅限 Windows) 使用預設處理常式啟動指定的檔案。指定啟動後停留在畫面上的喜好設定,並要求特定視窗大小。

Windows 8.1:  Windows 8.1 和 Windows Server 2012 R2 之前的版本不支援 LauncherOptions.DesiredRemainingView

Windows Phone:  Windows Phone 不支援 LauncherOptions.DesiredRemainingView

 

Default launch

呼叫 Windows.System.Launcher.launchFileAsync(IStorageFile) 方法來啟動預設應用程式。這個範例使用 Windows.Storage.StorageFolder.getFileAsync 方法來啟動影像檔案 test.png,它包含於應用程式套件內。


// Path to the file in the app package to launch
var imageFile = "images\\test.png";

// Get the image file from the package's image directory
Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(imageFile).then(
  function (file) {
    // Launch the retrieved file using the default app
    Windows.System.Launcher.launchFileAsync(file).then(
      function (success) {
        if (success) {
            // File launched
        } else {
            // File launch failed
        }
      });
  });

Open with launch

呼叫 Windows.System.Launcher.launchFileAsync(IStorageFile, LauncherOptions) 方法並將 LauncherOptions.displayApplicationPicker 設定為 true,以啟動使用者從 [開啟檔案] 對話方塊中選取的應用程式。

建議您在使用者不想選取特定檔案預設的應用程式時,使用 [開啟檔案]**** 對話方塊。 例如,如果您的應用程式允許使用者啟動影像檔案,則預設處理常式可能是某個檢視器應用程式。 在某些情況下,使用者可能想編輯影像而非檢視影像。使用 [開啟檔案] 選項搭配 [AppBar]**** 或操作功能表中的其他命令,可以讓使用者開啟 [開啟檔案] 對話方塊,並選取想在這類情況中使用的編輯器應用程式。

啟動 .png 檔案的 [開啟檔案] 對話方塊。這個對話方塊包含一個核取方塊,指定要將使用者的選擇用於所有 .png 檔案還是只用於這一個 .png 檔案。對話方塊中包含啟動檔案的四個應用程式選項以及一個 [更多選項] 連結。


// Path to the file in the app package to launch
var imageFile = "images\\test.png";

// Get the image file from the package's image directory
Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(imageFile).then(
  function (file) {
    // Set the show picker option
    var options = new Windows.System.LauncherOptions();
    options.displayApplicationPicker = true;

    // Launch the retrieved file using the selected app
    Windows.System.Launcher.launchFileAsync(file, options).then(
      function (success) {
        if (success) {
            // File launched
        } else {
            // File launch failed
        }
      });
  });

Launch with a recommended app fallback

在某些情況下,使用者可能尚未安裝可處理您要啟動之檔案的應用程式。依照預設,作業系統處理這些情況的方法是提供連結,讓使用者在市集上搜尋適當的應用程式。如果您想提供使用者在此情況下應取得何種應用程式的特定建議,可以在啟動檔案時一併傳送該建議。若要這樣做,請呼叫 Windows.System.Launcher.launchFileAsync(IStorageFile, LauncherOptions) 方法,並將 LauncherOptions.preferredApplicationPackageFamilyName 設為您想建議使用者使用的市集應用程式套件系列名稱。 然後將 LauncherOptions.preferredApplicationDisplayName 設為該應用程式的名稱。作業系統會使用此資訊,並搭配從市集取得建議應用程式的特定選項,以取代在市集中搜尋應用程式的一般選項。

注意  您必須設定這兩個選項才能建議應用程式。只設定其中一個將出現錯誤。

 

啟動 .contoso 檔案的 [開啟檔案] 對話方塊。因為 .contoso 未在電腦上安裝處理常式,所以對話方塊上出現一個選項,這個選項會包含市集圖示和文字以指示使用者市集中正確的處理常式。對話方塊中也包含了 [更多選項] 連結。


// Path to the file in the app package to launch
var imageFile = "images\\test.contoso";

// Get the image file from the package's image directory
Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(imageFile).then(
  function (file) {
    // Set the recommended app
    var options = new Windows.System.LauncherOptions();
    options.preferredApplicationPackageFamilyName = "Contoso.FileApp_8wknc82po1e";
    options.preferredApplicationDisplayName = "Contoso File App";


    // Launch the retrieved file pass in the recommended app 
    // in case the user has no apps installed to handle the file
    Windows.System.Launcher.launchFileAsync(file, options).then(
      function (success) {
        if (success) {
            // File launched
        } else {
            // File launch failed
        }
      });
  });

以所需的剩餘檢視啟動 (僅限 Windows)

呼叫 LaunchFileAsync 的來源應用程式可要求在檔案啟動後停留在畫面上。根據預設,Windows 會嘗試將所有可用空間平均分享給來源應用程式與用來處理檔案的目標應用程式。來源應用程式可以使用 DesiredRemainingView 屬性,告知作業系統要讓應用程式視窗佔用較多或較少可用空間。您也可以使用 DesiredRemainingView,指出來源應用程式在檔案啟動後不需要停留在畫面上,且可由目標應用程式完全取代。這個屬性只會指定發出呼叫的應用程式的慣用視窗大小。它不會指定其他可能也同時在螢幕上之應用程式的行為。

注意  Windows 在判斷來源應用程式的最終視窗大小時,會考量多種不同因素,例如來源應用程式的喜好設定、螢幕上的應用程式數目、螢幕方向等。設定 DesiredRemainingView 並無法保證來源應用程式的特定視窗行為。

 

Windows 8.1: Windows 8.1 和 Windows Server 2012 R2 之前的版本不支援 LauncherOptions.DesiredRemainingView

Windows Phone: Windows Phone 不支援 LauncherOptions.DesiredRemainingView

// Path to the file in the app package to launch
var imageFile = "images\\test.png";

// Get the image file from the package's image directory
Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(imageFile).done(
  function (file) {
    // Set the desired remaining view
    var options = new Windows.System.LauncherOptions();
    options.desiredRemainingView = Windows.UI.ViewManagement.ViewSizePreference.useLess;

    // Launch the retrieved file using the selected app
    Windows.System.Launcher.launchFileAsync(file, options).done(
      function (success) {
        if (success) {
            // File launched
        } else {
            // File launch failed
        }
      });
  });

備註

您的應用程式不能選取已啟動的應用程式。使用者決定要啟動哪個應用程式。使用者可以選取 Windows 市集應用程式或傳統型應用程式。

啟動檔案時,您的應用程式必須是前景應用程式,也就是說,使用者必須看得到您的應用程式。這項需求可讓使用者握有控制權。為了滿足這項需求,請務必將所有檔案啟動直接繫結到您的應用程式 UI。 最好是讓使用者一律必須採取某些動作才能起始檔案啟動。如果您嘗試啟動檔案,而您的應用程式不在前景中執行,則啟動將會失敗,並會叫用您的錯誤回呼。

如果包含程式碼或指令碼的檔案類型 (例如 .exe、.msi 以及 .js 檔案) 是由作業系統自動執行,您就無法啟動這些檔案類型。這項限制可以防止使用者遭受可能竄改作業系統的惡意檔案攻擊。如果包含指令碼的檔案類型 (例如 .docx 檔案) 是由隔離指令碼的應用程式所執行,就可以使用這個方法來啟動。Microsoft Word 之類的應用程式會防止 .docx 檔案中的指令碼修改作業系統。

如果您嘗試啟動受限制的檔案類型,則啟動將會失敗,並會叫用您的錯誤回呼。如果您的應用程式處理許多不同類型的檔案,而您預期會碰到這個錯誤,建議您為使用者提供遞補做法。例如,您可以為使用者提供將檔案儲存到桌面的選項,讓他們從桌面開啟檔案。

完整範例

請參閱關聯啟動範例 (Windows)

相關主題

工作

如何處理檔案啟用

如何啟動 URI 的預設應用程式

指導方針

檔案類型與 URI 的指導方針和檢查清單

參考

Windows.Storage.StorageFile

Windows.System.Launcher.launchFileAsync