FileOpenPicker 類別

定義

表示 UI 元素,可讓使用者選擇並開啟檔案。

在傳統型應用程式中,在以顯示 UI 的方式使用這個類別的實例之前,您必須將物件與其擁有者的視窗控制碼產生關聯。 如需詳細資訊和程式碼範例,請參閱 顯示相依于 CoreWindow 的 WinRT UI 物件

public ref class FileOpenPicker sealed
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
class FileOpenPicker final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class FileOpenPicker final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class FileOpenPicker final
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public sealed class FileOpenPicker
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class FileOpenPicker
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class FileOpenPicker
function FileOpenPicker()
Public NotInheritable Class FileOpenPicker
繼承
Object Platform::Object IInspectable FileOpenPicker
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

檔案 選擇器範例 示範如何檢查應用程式是否已貼齊、如何設定檔案選擇器屬性,以及如何顯示檔案選擇器,讓使用者可以挑選一個檔案。

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");

StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
    // Application now has read/write access to the picked file
    OutputTextBlock.Text = "Picked photo: " + file.Name;
}
else
{
    OutputTextBlock.Text = "Operation cancelled.";
}
// Create the picker object and set options
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
// Users expect to have a filtered view of their folders depending on the scenario.
// For example, when choosing a documents folder, restrict the filetypes to documents for your application.
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

// Open the picker for the user to pick a file
openPicker.pickSingleFileAsync().then(function (file) {
    if (file) {
        // Application now has read/write access to the picked file
        WinJS.log && WinJS.log("Picked photo: " + file.name, "sample", "status");
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});
internal bool EnsureUnsnapped()
{
    // FilePicker APIs will not work if the application is in a snapped state.
    // If an app wants to show a FilePicker while snapped, it must attempt to unsnap first
    bool unsnapped = ((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap());
    if (!unsnapped)
    {
        NotifyUser("Cannot unsnap the sample.", NotifyType.StatusMessage);
    }

    return unsnapped;
}

注意

不論使用者挑選單一檔案或多個檔案,您一律應該確定您的應用程式不會 (或無法解壓縮) 並設定檔案選擇器屬性。

IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();
if (files.Count > 0)
{
    StringBuilder output = new StringBuilder("Picked files:\n");
    // Application now has read/write access to the picked file(s)
    foreach (StorageFile file in files)
    {
        output.Append(file.Name + "\n");
    }
    OutputTextBlock.Text = output.ToString();
}
else
{
    OutputTextBlock.Text = "Operation cancelled.";
}
openPicker.pickMultipleFilesAsync().then(function (files) {
    if (files.size > 0) {
        // Application now has read/write access to the picked file(s)
        var outputString = "Picked files:\n";
        for (var i = 0; i < files.size; i++) {
            outputString = outputString + files[i].name + "\n";
        }
        WinJS.log && WinJS.log(outputString, "sample", "status");
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});

備註

若要開始存取檔案和資料夾檔案選擇器,請參閱 快速入門:使用檔案選擇器存取檔案

如需如何在 UWP 應用程式外部使用此 API 的資訊,請參閱 從 .NET 應用程式呼叫 Interop API

版本歷程記錄

Windows 版本 SDK 版本 新增值
1903 18362 CreateForUser
1903 18362 User

建構函式

FileOpenPicker()

建立 FileOpenPicker的新實例。

在傳統型應用程式中,在以顯示 UI 的方式使用這個類別的實例之前,您必須將物件與其擁有者的視窗控制碼產生關聯。 如需詳細資訊和程式碼範例,請參閱 顯示相依于 CoreWindow 的 WinRT UI 物件

屬性

CommitButtonText

取得或設定檔案開啟選擇器認可按鈕的標籤文字。

ContinuationData

取得應用程式在 PickSingleFileAndContinuePickMultipleFilesAndContinue 作業之前要填入的一組值,以停用應用程式,以便在應用程式啟動時提供內容。 (Windows Phone 8.x 應用程式)

FileTypeFilter

取得檔案開啟選擇器所顯示的檔案類型集合。

SettingsIdentifier

取得或設定與檔案開啟選擇器狀態相關聯的設定識別碼。

SuggestedStartLocation

取得或設定檔案開啟選擇器尋找要呈現給使用者之檔案的初始位置。

User

取得 建立 FileOpenPicker 之使用者的相關資訊。 針對 多使用者應用程式使用這個屬性。

ViewMode

取得或設定檔案開啟選擇器用來顯示專案的檢視模式。

方法

CreateForUser(User)

建立 FileOpenPicker ,範圍設定為指定使用者的個人目錄。 針對 多使用者應用程式使用這個方法。

PickMultipleFilesAndContinue()

從 Windows 10 淘汰;請改用 PickSingleFolderAsync。 顯示檔案選擇器,讓使用者可以挑選多個檔案、停用和應用程式,並在作業完成時重新啟用它。 (Windows Phone 8.x 應用程式)

PickMultipleFilesAsync()

顯示檔案選擇器,讓使用者可以挑選多個檔案。 (UWP app)

PickSingleFileAndContinue()

顯示檔案選擇器,讓使用者可以挑選一個檔案、可能停用應用程式,並在作業完成時重新啟用它。 (Windows Phone 8.x 應用程式)

PickSingleFileAsync()

顯示檔案選擇器,讓使用者可以挑選一個檔案。

PickSingleFileAsync(String)

顯示檔案選擇器,讓使用者可以挑選一個檔案。

ResumePickSingleFileAsync()

如果使用者在選擇器作業完成且應用程式暫停後離開應用程式,繼續挑選作業以擷取使用者選取的檔案。

適用於

另請參閱